Android Api Demos登顶之路(五十)Notification-->Service Controller
2015-08-27 00:34
375 查看
这个demo演示了如何利用服务发送消息通知
activity_main.xml
NotificationService
MainActivity
配置文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/start" android:text="Start Service" android:layout_gravity="center_horizontal"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stop" android:text="Stop Service" android:layout_gravity="center_horizontal"/> </LinearLayout>
NotificationService
public class NotificationService extends Service { private static final int MOOD_NOTIFICATION = R.id.start; // ConditionVariable用于线程同步 private ConditionVariable mCondition; private NotificationManager nm; @Override public void onCreate() { super.onCreate(); // 初始化一些变量 nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mCondition = new ConditionVariable(false); Thread notifyThread = new Thread(mTask, "NotificationService"); notifyThread.start(); } private Runnable mTask = new Runnable() { @Override public void run() { for (int i = 0; i < 4; i++) { showNotification(R.drawable.star_big_on, "OK"); //如果阻塞在5秒内被打开了,则跳出循环 if(mCondition.block(5*1000)){ break; } showNotification(R.drawable.icon48x48_2, "Happy"); //如果阻塞在5秒内被打开了,则跳出循环 if(mCondition.block(5*1000)){ break; } showNotification(R.drawable.ic_launcher, "Sad"); //如果阻塞在5秒内被打开了,则跳出循环 if(mCondition.block(5*1000)){ break; } } //循环结束后则结束服务 NotificationService.this.stopSelf(); } }; //这个对象接收客户端的交互动作 private final IBinder mBinder = new Binder() { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { return super.onTransact(code, data, reply, flags); } }; @Override public IBinder onBind(Intent intent) { return mBinder; } protected void showNotification(int moodId, String text) { // 点击消息时启动自己的主Activity,最后一个参数设为0表示使用系统默认的标识符 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); Notification.Builder builder=new Builder(this); /* * 这里需要注意的是,在这里没对trickerText进行设值,而是通过在通知栏中图标的更新 * 变化来显示通知消息已经发生了改变。官方也强烈要求以这种方式进行设置(如同网络状态的改变的通知) */ builder.setSmallIcon(moodId) .setWhen(System.currentTimeMillis()) .setContentTitle("Mood") .setContentText(text) .setDefaults(Notification.DEFAULT_ALL) .setContentIntent(contentIntent); Notification noti=builder.build(); nm.notify(MOOD_NOTIFICATION, noti); } @Override public void onDestroy() { super.onDestroy(); //取消通知消息 nm.cancel(MOOD_NOTIFICATION); //打开阻塞 mCondition.open(); } }
MainActivity
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button start=(Button) findViewById(R.id.start); Button stop=(Button) findViewById(R.id.stop); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startService(new Intent(MainActivity.this, NotificationService.class)); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { stopService(new Intent(MainActivity.this, NotificationService.class)); } }); } }
配置文件
<service android:name="com.fishtosky.notificationservice.NotificationService"></service>
相关文章推荐
- Android Studio Git初体验
- Android Supported Media Formats安卓支持的影片格式(一)
- Android三种实现定时器的方法
- [置顶] android设置一张图片透明度导致其在多个activity里均是透明的问题
- android canvas onDraw和Draw的bitmap区别
- Android通过startService实现批量下载示例
- Android 进程常驻----开篇
- ActionBar
- Android studio安装Genymotion插件
- Android学习笔记(十三)
- Android:ListView(一)
- Android Studio如何集成Genymotion
- android开源组件
- android 验证电话号码或者固定电话 均可
- Android Studio快捷键
- Android Studio快捷键
- Android Fragment实例
- Android Studio多个module导入AAR库
- Android Studio下使用GoAgent进行sdk更新
- Android中广播的使用(动态、静态注册的区别,有序无序广播的使用)