通过service和handler实现Notification在后台打开activity
来源:互联网 发布:js 仪表盘 编辑:程序博客网 时间:2024/05/19 23:13
一直以来都是在csdn查看各种大神的贡献资料,但自己却没有为他们做点什么。这几天在做一个即时聊天软件,需要实现一个功能:用户登录后,可以后台接收消息,通过Notification在消息栏提示,用户点击消息栏可以跳转到聊天界面。
为了模拟接收短信,我新建了三个Activity。在FirstActivity中我打开了service服务;在SecondActivity中我通过Handler的sendMessage方法发送数据,模拟收到短信;在第NotificationService.java类中,我通过重写Handler的handlerMessage方法,来实时监听,若收到sendMessage发送的数据,则跳转到ThirdActivity界面。Ok,闲话少说,直接上码:
Info.java:
package com.zr.android.info;import android.os.Handler;public class Info {/** 实现界面更新 */public static Handler jumpHandler = null;}
FirstActivity.java:
package com.zr.android;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FirstActivity extends Activity{Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first);button1 = (Button)findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {Intent intent = new Intent(FirstActivity.this, SecondActivity.class);startActivity(intent);//启动NotificationService Intent notificationServiceIntent = new Intent(); notificationServiceIntent.setAction("com.zr.android.service.NotificationService"); startService(notificationServiceIntent); }});}}
SecondActivity.java:
package com.zr.android;import com.zr.android.info.Info;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SecondActivity extends Activity{Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);button1 = (Button)findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {android.os.Message meetmsg = new android.os.Message();//创建handler消息 meetmsg.arg1=1; Info.jumpHandler.sendMessage(meetmsg);}});}}
ThirdActivity.java:
package com.zr.android;import android.app.Activity;import android.os.Bundle;public class ThirdActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.third);}}
NotificationService.java
package com.zr.android.service;import com.zr.android.R;import com.zr.android.ThirdActivity;import com.zr.android.info.Info;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;public class NotificationService extends Service{static final int NOTIFICATION_ID = 0x1123;@Overridepublic IBinder onBind(Intent intent) {return null;} @Override public void onCreate() { super.onCreate(); Info.jumpHandler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.arg1==1){ //开启一个对话框或者是对话框形式的activity //创建一个启动其他Activity的Intent Intent intent = new Intent(getApplicationContext(), ThirdActivity.class); PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent , 0); //创建一个Notification Notification notify = new Notification(); //为Notification设置图标,该图标显示在状态栏 notify.icon = R.drawable.notify; //为Notification设置文本内容,该文本会显示在状态栏 notify.tickerText = "启动其他Activity的通知"; //为Notification设置发送时间 notify.when = System.currentTimeMillis(); //为Notification设置声音 notify.defaults = Notification.DEFAULT_SOUND; //为Notification设置默认声音、默认振动、默认闪光灯 notify.defaults = Notification.DEFAULT_ALL; //点击后关闭notifycation notify.flags=Notification.FLAG_AUTO_CANCEL; //设置事件信息 notify.setLatestEventInfo(getApplicationContext(), "普通通知","点击查看", pi); //获取系统的NotificationManager服务 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //发送通知 notificationManager.notify(NOTIFICATION_ID, notify); } super.handleMessage(msg); } }; } @Override public void onDestroy() { super.onDestroy(); }}
first.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="1111111111111111111" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启service" /></LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="22222222222222222" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送handler信息" /></LinearLayout>
third.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="333333333333333333" /></LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zr.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".FirstActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity"></activity> <activity android:name=".ThirdActivity"></activity> <service android:name="com.zr.android.service.NotificationService"> <intent-filter> <action android:name="com.zr.android.service.NotificationService" /> </intent-filter> </service> </application><!-- 添加操作闪光灯的权限 --><uses-permission android:name="android.permission.FLASHLIGHT"/><!-- 添加操作振动器的权限 --><uses-permission android:name="android.permission.VIBRATE"/></manifest>
如果有什么问题欢迎大家指正,我们一起探讨。还有Notification设置部分是参考《疯狂Android讲义》的,在这表示感谢。
- 通过service和handler实现Notification在后台打开activity
- 在后台Service中直接启动Activity
- Handler实现Activity和Service通信
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
- APP更新功能,使用Notification实现在后台更新
- Activity和Service之间通过handler传递数据
- 使用Handler实现Service和Activity之间的双向通信
- 使用Handler实现Activity和Service之间的交互
- 利用Handler实现Activity和Service之间通信
- 使用Handler实现Service和Activity之间的双向通信
- Android Service通过Handler与Activity交互
- Service里常驻Notification,可以在后台低内存的时候不被kill
- 系统运行在后台的Notification
- 在Activity和Service之外使用handler
- 保持service一直在后台运行
- 如何保持service长期在后台运行
- ORMLite在后台service中进行数据处理
- 如何保证service一直在后台运行
- Flex 与java通信的三种方式
- 学习笔记——JavaScript中的Array对象
- Android学习笔记---29_构建soap协议内容,发送xml数据和调用webservice,手机号码归属地查询器
- 对HashMap的两种遍历方法
- jQuery Frontier Calendar简介
- 通过service和handler实现Notification在后台打开activity
- XMOVE3.0手持终端——软件介绍(二):在2KB内存的单片机上实现的彩屏GUI控件库
- JAVA开源项目
- 【VB/.NET】Converting VB6 to VB.NET 【Part I】【之二】
- Inside the Linux Packet Filter
- python笔记之eclipse开发环境搭建
- ImageFeatures
- 腾讯编程马拉松复赛第三场 1 & 2 & 5
- 好久没有更新博客了