转于: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");
- }
- }
嗯,把发送通知的代码复制到这里循环几百次。。用来测试退出程序后服务是不是还在跑着。
相关推荐
android消息推送使用NotificationManager时间消息推送
NULL 博文链接:https://forlan.iteye.com/blog/2264722
要使用消息通知,必须要用到两个类:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通过notify方法来向android系统发送消息栏通知和显示。效果 :代码: 代码...
现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息。 1.注册一个Service import ...
在Android系统中,发一个状态栏通知还是很方便的。下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationManager 、 Notification。 ...
例如我们手机的短信系统,当不断有新消息传来时,我们的通知栏仅仅是更改传来短信的数目,而不是对每条短信单独做一个通知栏用于提示。 修改通知 可以设置一条通知,当然可以更新一条通知,我们通过在调用...
安卓通知栏实现,通过 NotificationManager实现管理 notification ,包括 发送通知和 取消通知;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); or Json2Notification.from(context)....
本文主要讲解状态栏通知Notification、NotificationManager,小编觉得非常详细,给大家一个参考,希望对大家学习有所帮助。
主要介绍了Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏,需要的朋友可以参考下
android 状态栏的图标与文字提醒 NotificationManager与Notification
Android Service服务启动与绑定用法测试,这个android源码演示了如何启动和停止指定的android系统服务,如何Bind Server绑定服务和如何unBind Server解绑服务,并使用NotificationManager来弹出消息处理对话框。
Android 应用案例——雷电游戏,通过一个完整的雷电游戏,讲述了Android的游戏开发思路、框架和具体实现,并比较了Android游戏开发和Java ME游戏开发的异同,成功移植了Java ME游戏API到Android当中 第17章 Android...
NULL 博文链接:https://hualikejava.iteye.com/blog/953947
本文实例讲述了Android开发之使用通知栏显示提醒信息的方法。分享给大家供大家参考,具体如下: 用通知栏来提醒 public void notifyKJ() { //获得通知管理器,通知是一项系统服务 NotificationManager manager = ...
之前网上看了下自定义消息栏,通知栏,了解到了Notification这个控件,发现UC浏览器等都是这种类型,今天写个demo实现下,如图: 其中每个按钮都有不同的功能,代码如下: package com.example.textwsjdemo; ...
Android基础教程之----Android状态栏提醒(Notification,NotificationManager)的使用.doc Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!.doc Android基础教程之----动态更改屏幕方向的简单例子...
自学 Android 做的第二十七个 Android 程序,使用了 NotificationManager、Notification 和 Notification.Builder
4.2.21 状态栏提示(Notification、NotificationManager) 4.2.22 对话框中的进度条(ProgressDialog) 4.3 界面布局 4.3.1 垂直线性布局 4.3.2 水平线性布局 4.3.3.相对布局(RelativeLayout) 4.3.4 表单布局...