(4.3.1.2)【项目一】主体框架Fragment中启动Service与notification的使用
来源:互联网 发布:一剑轩辕坐骑进阶数据 编辑:程序博客网 时间:2024/05/22 01:54
- 创建Service
- 生成notification
- 示例
- 注册Service
- 启动Service
创建Service
public class MyService extends Service { public static final String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate() executed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand() executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy() executed"); } @Override public IBinder onBind(Intent intent) { return null; } }
生成notification
// 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification myNotify = new Notification(); notify1.setLatestEventInfo(this, "Notification Title", "This is the notification message", pendingIntent); manager.notify(NOTIFICATION_FLAG, notify1);
- (4.1.13)Android之Notification的多种用法
示例
package com.android.yunshi.service;//每日运程的信息推送import android.app.Service;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import com.android.yunshi.LogoActivity;import com.android.yunshi.R;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.content.SharedPreferences;import android.os.IBinder;import android.util.Log;import android.widget.RemoteViews;public class DailyYunChengTuiSong extends Service { public Timer timer; final String TAG = "com.yhf.dailyyunchengtuisong"; int CUSTOM_VIEW_ID = 1;// 通知的id号 Calendar calendar;// 时间类,设定timer.schedule的执行时间 Boolean isyunchengtuisong_Done; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onBind"); return null; } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onUnbind"); return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { // TODO Auto-generated method stub super.onRebind(intent); Log.i(TAG, "onRebind"); } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.i(TAG, "onCreate"); timer = new Timer(true);// 当程序只有daemon线程的时候,它就会自动终止运行。 } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "onDestroy"); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.i(TAG, "onStart"); // 设定执行时间为10:21分 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); // chedule方法, // 第一个参数是TimerTask对象 // 第二个参数表示开始执行前的延时时间,或者定时 timer.schedule(new TimerTask() { public void run() { int icon = R.drawable.yunshi360; CharSequence tickerText = "运势360"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.yunshi_more_tuisong_notification); contentView.setImageViewResource(R.id.image, R.drawable.yunshi360); contentView.setTextViewText(R.id.title, "每日运程"); contentView.setTextViewText(R.id.text, "记得开启运势360查看今日运程呦···"); notification.contentView = contentView; Intent notificationIntent = new Intent( DailyYunChengTuiSong.this, LogoActivity.class); PendingIntent contentIntent = PendingIntent .getActivity(DailyYunChengTuiSong.this, 0, notificationIntent, 0); notification.contentIntent = contentIntent; notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失 // 也可设置为不消失 notification.defaults =Notification.DEFAULT_SOUND; /* * 添加声音 * notification.defaults |=Notification.DEFAULT_SOUND; * 或者使用以下几种方式 * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT" * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音 */ /* * 添加振动 * notification.defaults |= Notification.DEFAULT_VIBRATE; * 或者可以定义自己的振动模式: * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒 * notification.vibrate = vibrate; * long数组可以定义成想要的任何长度 * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动 */ /* * 添加LED灯提醒 * notification.defaults |= Notification.DEFAULT_LIGHTS; * 或者可以自己的LED提醒模式: * notification.ledARGB = 0xff00ff00; * notification.ledOnMS = 300; //亮的时间 * notification.ledOffMS = 1000; //灭的时间 * notification.flags |= Notification.FLAG_SHOW_LIGHTS; */ String ns = NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.notify(CUSTOM_VIEW_ID, notification); // 停止服务 // 服务停止的时间不确定,后面的代码还是会执行,并且onDestroy()方法也会执行, // 下次重新启动服务的时候,先调用onCreate(),然后再调用onStart()方法。 DailyYunChengTuiSong.this.stopSelf(); } }, time); }}
注册Service
<service android:name="com.android.yunshi.service.DailyYunChengTuiSong" > <intent-filter> <action android:name="com.yhf.dailyyunchengtuisong" > </action> <category android:name="android.intent.category.default" /> </intent-filter> </service>
启动Service
intent.setAction("com.yhf.dailyyunchengtuisong"); startService(intent);
0 0
- (4.3.1.2)【项目一】主体框架Fragment中启动Service与notification的使用
- (4.3.1.3)【项目一】主体框架Fragment中Selector的使用与自定义button-shape
- (4.3.1.1)【项目一】主体框架FragmentActivity+FragmentTabHost
- Notification的使用(一)
- Fragment的解析与使用(一)
- Service及Notification的使用
- Service与Notification的结合使用实现文件下载
- Notification和NotificationManager的使用(一)
- Android通知Notification的使用(一)
- 【简单项目框架一】Fragment实现的底部导航
- 【简单项目框架一】Fragment实现的底部导航
- Android中notification的使用 【基于Mars-droid的MP3项目的notification使用】
- Glide源码解析篇之框架主体结构(一)
- Service的使用(二)启动服务与IntentService
- Android中Activity、Fragment与Service的生命周期
- Fragment的概念与使用(一)--基本概念
- Fragment的概念与使用(一)--基本概念
- Atitit 项目的主体设计与结构文档 v3
- [寒江孤叶丶的Cocos2d-x之旅_34]ODSocket(BSDSocket)如何在切换网络状态时自动重连
- afnetworking2.0源码解析AFURLRequestSerialization
- 一个程序员的顿悟
- hdu 1008 Elevator
- Yii模板头部引入css,尾部引入js文件
- (4.3.1.2)【项目一】主体框架Fragment中启动Service与notification的使用
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
- 【工作日志】【asp】【04】asp连接 sqlserver 2008的正确代码
- android Fragmentd的使用
- Project Euler:Problem 72 Counting fractions
- Activity 重载方法 onStart和onResume、onPause和onStop的区别
- Mongodb备份和还原
- GPUImage开源框架使用教程
- Apache如何配置SSL