转于:http://blog.csdn.net/huutu/article/details/12977645
?
------------------------------------------------------------------------------------------------------------------
?
Android Service ,在退出程序之后 ,仍然留在后台作为服务项做一些事情,比如说监听用户输入之类的。。。。
这里用来做后台的消息推送,像游戏中的广告可以这么做。
?
Android工程里面有两个Activity、一个Service重写。
MainActivity 是进入游戏的界面。
SecondActivity 是打开通知,点击推送的消息打开的Activity 。
push_service 是重写的Service 。
?
MainActivity
- package?com.example.androidnotification;??
- ??
- import?android.os.Bundle;??
- import?android.R.integer;??
- import?android.app.Activity;??
- import?android.app.Notification;??
- import?android.app.NotificationManager;??
- import?android.app.PendingIntent;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.util.Log;??
- import?android.view.Menu;??
- import?android.view.View;??
- import?android.view.View.OnClickListener;??
- import?android.widget.Button;??
- ??
- public?class?MainActivity?extends?Activity?{??
- ??????
- ????private?Button?button1;??
- ????private?Button?button2;??
- ??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_main);??
- ??????????
- ????????//获取到通知管理器??
- ????????NotificationManager?mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);??
- ??????????
- ????????//定义内容??
- ????????int?notificationIcon=R.drawable.icon;??
- ????????CharSequence?notificationTitle="测试通知栏--title";??
- ????????long?when?=?System.currentTimeMillis();??
- ??????????
- ????????Notification?notification=new?Notification(notificationIcon,?notificationTitle,?when);??
- ??????????
- ????????notification.defaults=Notification.DEFAULT_ALL;??
- ??????????
- ????????Intent?intent=new?Intent(MainActivity.this,SecondActivity.class);??
- ????????PendingIntent?pendingIntent=PendingIntent.getActivity(MainActivity.this,?0,?intent,?0);??
- ????????notification.setLatestEventInfo(this,"测试展开title",?"测试展开内容",pendingIntent);??
- ??????????
- ????????if(notification!=null)??
- ????????{??
- ????????????Log.e("notifacation",?"notifacation?is?ok");??
- ????????????mNotificationManager.notify(1000,?notification);??
- ????????}??
- ??????????
- ????????button1=(Button)findViewById(R.id.button1);??
- ????????button1.setOnClickListener(new?OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?arg0)?{??
- ????????????????//?TODO?Auto-generated?method?stub??
- ????????????????Intent?pushIntent=new?Intent("com.example.androidnotification.push_service");??
- ????????????????startService(pushIntent);??
- ??????????????????
- ????????????}??
- ????????});??
- ??????????
- ????????button2=(Button)findViewById(R.id.button2);??
- ????????button2.setOnClickListener(new?OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????//?TODO?Auto-generated?method?stub??
- ????????????????MainActivity.this.finish();??
- ????????????}??
- ????????});??
- ??????????
- ????}??
- ??
- ????@Override??
- ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
- ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
- ????????getMenuInflater().inflate(R.menu.main,?menu);??
- ????????return?true;??
- ????}??
- ??
- }??
打开程序的时候发送了一个通知。。。
主要就是获取到通知管理器然后新建一个通知,然后发送这个通知!。。。
?
?
SecondActivity?
- package?com.example.androidnotification;??
- ??
- import?android.app.Activity;??
- import?android.os.Bundle;??
- ??
- public?class?SecondActivity?extends?Activity?{??
- ??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.secondactivity);??
- ????}??
- }??
?
第二个Activity只是为了存在而存在,没有啥不懂。。
?
push_service
重写了Service ,注册一个Service 。。
- package?com.example.androidnotification;??
- ??
- import?android.app.Activity;??
- import?android.app.Notification;??
- import?android.app.NotificationManager;??
- import?android.app.PendingIntent;??
- import?android.app.Service;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.os.Handler;??
- import?android.os.IBinder;??
- import?android.util.Log;??
- ??
- public?class?push_service?extends?Service?{??
- ??
- ????@Override??
- ????public?IBinder?onBind(Intent?arg0)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????return?null;??
- ????}??
- ??????
- ????@Override??
- ????public?void?onCreate()??
- ????{??
- ????????super.onCreate();??
- ????????Log.i("push_service",?"push_service?onCreate");??
- ????}??
- ??????
- ????@SuppressWarnings("deprecation")??
- ????@Override??
- ????public?void?onStart(Intent?intent,int?startId)??
- ????{??
- ????????super.onStart(intent,?startId);??
- ????????Log.i("push_service",?"push_service?start");??
- ??????????
- ????????new?Thread(new?Runnable()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?run()?{??
- ????????????????//?TODO?Auto-generated?method?stub??
- ????????????????try?{??
- ????????????????????for?(int?i?=?0;?i?<100;?i++)?{??
- ????????????????????????Thread.sleep(3000);??
- ????????????????????????Log.i("push_service",?"push_service?foreach");??
- ??????????????????????????
- ??????????????????????????
- ??????????????????????????
- ????????????????????????//获取到通知管理器??
- ????????????????????????NotificationManager?mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);??
- ??????????????????????????
- ????????????????????????//定义内容??
- ????????????????????????int?notificationIcon=R.drawable.icon;??
- ????????????????????????CharSequence?notificationTitle="测试通知栏--title";??
- ????????????????????????long?when?=?System.currentTimeMillis();??
- ??????????????????????????
- ????????????????????????Notification?notification=new?Notification(notificationIcon,?notificationTitle,?when);??
- ??????????????????????????
- ????????????????????????notification.defaults=Notification.DEFAULT_ALL;??
- ??????????????????????????
- ????????????????????????Intent?intent=new?Intent(getApplicationContext(),SecondActivity.class);??
- ????????????????????????PendingIntent?pendingIntent=PendingIntent.getActivity(getApplicationContext(),?0,?intent,?0);??
- ????????????????????????notification.setLatestEventInfo(getApplicationContext(),"测试展开title",?"测试展开内容",pendingIntent);??
- ??????????????????????????
- ????????????????????????if(notification!=null)??
- ????????????????????????{??
- ????????????????????????????Log.e("notifacation",?"notifacation?is?ok");??
- ????????????????????????????mNotificationManager.notify(1000+i,?notification);??
- ????????????????????????}??
- ??????????????????????????
- ??????????????????????????
- ????????????????????}??
- ??????????????????????
- ????????????????}?catch?(InterruptedException?e)?{??
- ????????????????????//?TODO?Auto-generated?catch?block??
- ????????????????????e.printStackTrace();??
- ????????????????}??
- ??
- ????????????}??
- ????????}).start();??
- ????}??
- ??????
- ????@Override??
- ????public?void?onDestroy()??
- ????{??
- ????????super.onDestroy();??
- ????????Log.i("push_service",?"push_service?destroy");??
- ????}??
- ??
- }??
?
嗯,把发送通知的代码复制到这里循环几百次。。用来测试退出程序后服务是不是还在跑着。
?
?