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;@Overrideprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent pushIntent=new Intent("com.example.androidnotification.push_service");startService(pushIntent);}});button2=(Button)findViewById(R.id.button2);button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubMainActivity.this.finish();}});}@Overridepublic 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 {@Overrideprotected 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 {@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate(){super.onCreate();Log.i("push_service", "push_service onCreate");}@SuppressWarnings("deprecation")@Overridepublic void onStart(Intent intent,int startId){super.onStart(intent, startId);Log.i("push_service", "push_service start");new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 blocke.printStackTrace();}}}).start();}@Overridepublic void onDestroy(){super.onDestroy();Log.i("push_service", "push_service destroy");}}

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


更多相关文章

  1. 【Android(安卓)开发教程】Toast通知
  2. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  3. 细数Android(安卓)Studio中使用junit4测试框架中的坑
  4. android 下写文件性能测试
  5. android 单元测试
  6. Android(安卓)-- Android(安卓)JUint 与 Sqlite
  7. Android(安卓)单元测试小总结
  8. 使用uiautomatorviewer和uiautomator来做android的UI测试
  9. 【android测试】值得学习的android测试知识连接

随机推荐

  1. Android从相机或相册获取图片裁剪
  2. MediaScannerReceiver
  3. android一种较为复杂的布局
  4. Android隐藏软键盘
  5. Android(安卓)使用ContentProvider 添加
  6. Android搜索框自动提示文本框——(单一提
  7. Android(安卓)source download for linux
  8. Android(安卓)WiFi管理(WIFI_SERVICE)
  9. Android进度条简单练习实例
  10. How to resign the Android(安卓)APK