`

Android 本地推送消息到通知栏 NotificationManager 、注册Android Service

 
阅读更多

转于:http://blog.csdn.net/huutu/article/details/12977645

 

------------------------------------------------------------------------------------------------------------------

 

Android Service ,在退出程序之后 ,仍然留在后台作为服务项做一些事情,比如说监听用户输入之类的。。。。

这里用来做后台的消息推送,像游戏中的广告可以这么做。

 

Android工程里面有两个Activity、一个Service重写。

MainActivity 是进入游戏的界面。

SecondActivity 是打开通知,点击推送的消息打开的Activity 。

push_service 是重写的Service 。

 

MainActivity

[java] view plaincopy
  1. package com.example.androidnotification;  
  2.   
  3. import android.os.Bundle;  
  4. import android.R.integer;  
  5. import android.app.Activity;  
  6. import android.app.Notification;  
  7. import android.app.NotificationManager;  
  8. import android.app.PendingIntent;  
  9. import android.content.Context;  
  10. import android.content.Intent;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16.   
  17. public class MainActivity extends Activity {  
  18.       
  19.     private Button button1;  
  20.     private Button button2;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         //获取到通知管理器  
  28.         NotificationManager mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  
  29.           
  30.         //定义内容  
  31.         int notificationIcon=R.drawable.icon;  
  32.         CharSequence notificationTitle="测试通知栏--title";  
  33.         long when = System.currentTimeMillis();  
  34.           
  35.         Notification notification=new Notification(notificationIcon, notificationTitle, when);  
  36.           
  37.         notification.defaults=Notification.DEFAULT_ALL;  
  38.           
  39.         Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
  40.         PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this0, intent, 0);  
  41.         notification.setLatestEventInfo(this,"测试展开title""测试展开内容",pendingIntent);  
  42.           
  43.         if(notification!=null)  
  44.         {  
  45.             Log.e("notifacation""notifacation is ok");  
  46.             mNotificationManager.notify(1000, notification);  
  47.         }  
  48.           
  49.         button1=(Button)findViewById(R.id.button1);  
  50.         button1.setOnClickListener(new OnClickListener() {  
  51.               
  52.             @Override  
  53.             public void onClick(View arg0) {  
  54.                 // TODO Auto-generated method stub  
  55.                 Intent pushIntent=new Intent("com.example.androidnotification.push_service");  
  56.                 startService(pushIntent);  
  57.                   
  58.             }  
  59.         });  
  60.           
  61.         button2=(Button)findViewById(R.id.button2);  
  62.         button2.setOnClickListener(new OnClickListener() {  
  63.               
  64.             @Override  
  65.             public void onClick(View v) {  
  66.                 // TODO Auto-generated method stub  
  67.                 MainActivity.this.finish();  
  68.             }  
  69.         });  
  70.           
  71.     }  
  72.   
  73.     @Override  
  74.     public boolean onCreateOptionsMenu(Menu menu) {  
  75.         // Inflate the menu; this adds items to the action bar if it is present.  
  76.         getMenuInflater().inflate(R.menu.main, menu);  
  77.         return true;  
  78.     }  
  79.   
  80. }  

打开程序的时候发送了一个通知。。。

主要就是获取到通知管理器然后新建一个通知,然后发送这个通知!。。。

 

 

SecondActivity 

[java] view plaincopy
  1. package com.example.androidnotification;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class SecondActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.secondactivity);  
  13.     }  
  14. }  

 

第二个Activity只是为了存在而存在,没有啥不懂。。

 

push_service

重写了Service ,注册一个Service 。。

[java] view plaincopy
  1. package com.example.androidnotification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.app.Service;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.os.Handler;  
  11. import android.os.IBinder;  
  12. import android.util.Log;  
  13.   
  14. public class push_service extends Service {  
  15.   
  16.     @Override  
  17.     public IBinder onBind(Intent arg0) {  
  18.         // TODO Auto-generated method stub  
  19.         return null;  
  20.     }  
  21.       
  22.     @Override  
  23.     public void onCreate()  
  24.     {  
  25.         super.onCreate();  
  26.         Log.i("push_service""push_service onCreate");  
  27.     }  
  28.       
  29.     @SuppressWarnings("deprecation")  
  30.     @Override  
  31.     public void onStart(Intent intent,int startId)  
  32.     {  
  33.         super.onStart(intent, startId);  
  34.         Log.i("push_service""push_service start");  
  35.           
  36.         new Thread(new Runnable() {  
  37.               
  38.             @Override  
  39.             public void run() {  
  40.                 // TODO Auto-generated method stub  
  41.                 try {  
  42.                     for (int i = 0; i <100; i++) {  
  43.                         Thread.sleep(3000);  
  44.                         Log.i("push_service""push_service foreach");  
  45.                           
  46.                           
  47.                           
  48.                         //获取到通知管理器  
  49.                         NotificationManager mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  
  50.                           
  51.                         //定义内容  
  52.                         int notificationIcon=R.drawable.icon;  
  53.                         CharSequence notificationTitle="测试通知栏--title";  
  54.                         long when = System.currentTimeMillis();  
  55.                           
  56.                         Notification notification=new Notification(notificationIcon, notificationTitle, when);  
  57.                           
  58.                         notification.defaults=Notification.DEFAULT_ALL;  
  59.                           
  60.                         Intent intent=new Intent(getApplicationContext(),SecondActivity.class);  
  61.                         PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);  
  62.                         notification.setLatestEventInfo(getApplicationContext(),"测试展开title""测试展开内容",pendingIntent);  
  63.                           
  64.                         if(notification!=null)  
  65.                         {  
  66.                             Log.e("notifacation""notifacation is ok");  
  67.                             mNotificationManager.notify(1000+i, notification);  
  68.                         }  
  69.                           
  70.                           
  71.                     }  
  72.                       
  73.                 } catch (InterruptedException e) {  
  74.                     // TODO Auto-generated catch block  
  75.                     e.printStackTrace();  
  76.                 }  
  77.   
  78.             }  
  79.         }).start();  
  80.     }  
  81.       
  82.     @Override  
  83.     public void onDestroy()  
  84.     {  
  85.         super.onDestroy();  
  86.         Log.i("push_service""push_service destroy");  
  87.     }  
  88.   
  89. }  

 

嗯,把发送通知的代码复制到这里循环几百次。。用来测试退出程序后服务是不是还在跑着。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics