返回首页

介绍
本文介绍如何下载多个文件,使用进度,通知和AsyncTask。使用代码要做的第一件事是检查连接使用的isInternetConnectionActive的(背景)方法。如果此方法返回true,然后创建一个显示消息的通知:

private static final int HELLO_ID = 1;

    

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

        

    if(isInternetConnectionActive(getApplicationContext())) {

     	showNotification(1);

    }

    else {

    	showNotification(0);

    }

}

     

private boolean isInternetConnectionActive(Context context) {

    	NetworkInfo networkInfo = ((ConnectivityManager) context

    	    .getSystemService(Context.CONNECTIVITY_SERVICE))

    	    .getActiveNetworkInfo();



  if(networkInfo == null || !networkInfo.isConnected()) {

     return false;

  }

     return true;

}

    

private void showNotification(int status){

    	String ns = Context.NOTIFICATION_SERVICE;

   NotificationManager mNotificationManager = 

       (NotificationManager) getSystemService(ns);



   int icon = android.R.drawable.ic_media_play;

   CharSequence ticketText = "Connection";

   long when = System.currentTimeMillis();



   Notification notification = new Notification(icon, ticketText, when);



   Context context = getApplicationContext();

   CharSequence contentTitle = "Internet Connection";

   CharSequence contentText = "";

        

   if(status == 1){

     	contentText = "Internet connected, Are you Update your Application?";

   }

   else{

    	contentText = "Internet disconnected!";

   }

       

   Intent notificationIntent = new Intent(this, NotifyPage.class);

   notificationIntent.addFlags(Notification.FLAG_ONGOING_EVENT);

   PendingIntent contentIntent = 

     PendingIntent.getActivity(this, 0, notificationIntent, 0);



   notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);



   mNotificationManager.notify(HELLO_ID, notification);

}
在showNotification()方法,我们定义了一个意向书,宣布其第二个参数作为NotifyPage.class。当用户选择了一个通知,请致电活动NotifyPage。这项活动,设计一个XML文件,其中包括两个按钮:一个按钮,创建另一个用于显示一个ProgressBar()和一个按钮来取消下载操作和回家的通知。{C}在DownloadTask类,你可以发送多个文件,但在这个例子中,我们只发送文件的地址。事实上,执行(URL),你叫doInBackground()运行在后台下载的操作。您可以下载使用四种在DownloadTask类的方法调用通知:
public  DownloadTask(Context context){

   this.context = context;

   //Create the notification object from NotificationHelper class



   mNotificationHelper = new NotificationHelper(context);

}



protected void onPreExecute(){

   //Create the notification in the statusbar

   mNotificationHelper.createNotification();

}



protected void onProgressUpdate(Integer... progress) {

   //This method runs on the UI thread, it receives progress updates

   //from the background thread and publishes them to the status bar

   mNotificationHelper.progressUpdate(progress[0]);

}

protected void onPostExecute(Void result)    {

   //The task is complete, tell the status bar about it

   mNotificationHelper.completed();

}

下次来定义连接的Andr​​oidManifest.xml权限:android.permission.INTERNET的 - android.permission.ACCESS_NETWORK_STATE。景点
希望这篇文章教你如何创建一个状态栏的通知,并使用下载或上传文件1 AsyncTask类。| omid.nazifi:davidlow70

回答

评论会员:D 时间:2
"