12 StatusBar

 1 package com.szy.status;
 2 
 3 import android.app.IntentService;
 4 import android.app.Notification;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Intent;
 8 import android.util.Log;
 9 
10 /**
11  * @author coolszy
12  * @blog http://blog.csdn.net/coolszy
13  */
14 public class StatusService extends IntentService
15 {
16     private static final String TAG = "StatusService";
17 
18     // private static final int KUKA = 0;
19 
20     public StatusService()
21     {
22         super("StatusService");
23     }
24 
25     @Override
26     protected void onHandleIntent(Intent intent)
27     {
28         Log.i(TAG, "开始下载....");
29         showNotification(false);
30         try
31         {
32             Thread.sleep(10000);
33             showNotification(true);
34         } catch (InterruptedException e)
35         {
36             e.printStackTrace();
37         }
38         Log.i(TAG, "程序下载完毕");
39     }
40 
41     private void showNotification(boolean finish)
42     {
43         Notification notification;
44         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
45         Intent intent = new Intent(this, MainActivity.class);
46         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
47         if (!finish)
48         {
49             notification = new Notification(R.drawable.head, "开始下载", System.currentTimeMillis());
50             notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent);
51         }
52         else
53         {
54             notification = new Notification(R.drawable.head, "下载完毕", System.currentTimeMillis());
55             notification.setLatestEventInfo(this, "下载", "程序下载完毕", contentIntent);
56         }
57         notification.defaults=Notification.DEFAULT_ALL;
58         manager.notify(R.layout.main, notification);
59         
60     }
61 
62 }
statusService.java
 1 package com.szy.status;
 2 
 3 import android.app.Activity;
 4 import android.app.NotificationManager;
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 
11 public class MainActivity extends Activity
12 {
13     private Button btnStartService;
14     @Override
15     public void onCreate(Bundle savedInstanceState)
16     {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19         btnStartService=(Button)findViewById(R.id.btnStartService);
20         btnStartService.setOnClickListener(new OnClickListener()
21         {
22             @Override
23             public void onClick(View v)
24             {
25                 Intent intent=new Intent(MainActivity.this, StatusService.class);
26                 startService(intent);
27             }
28         });
29     }
30     
31     @Override
32     protected void onStart()
33     {
34         super.onStart();
35         NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
36         manager.cancel(R.layout.main);
37     }
38     
39     
40 }
MainActivity.java

 

posted @ 2014-04-29 08:40  阿黄的苹果  阅读(138)  评论(0编辑  收藏  举报