[Android]Broadcast,Intent,PendingIntent,Handler例子
来源:互联网 发布:电脑笔记软件 编辑:程序博客网 时间:2024/06/07 01:58
- 代码
- 知识点
- Handler
- IntentPendingIntent
- BroadcastReceiver
- Alarm
代码
先提供代码实例,代码运行ok,需要自己再添加一个button。
package com.example.setalarmtest;import android.app.Activity;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Looper;import android.os.Message;import android.os.PowerManager;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { String TAG = "setAlarm"; private static final String PENDING_INTENT_ALARM = "Pending_Intent_Alarm"; public long alarmTime = 5*1000; Boolean bTigger = true; Button mBtSendMsg; Bundle extras = new Bundle(); static final int MESSAGE_TIGGER_STEP_1 = 1; static final int MESSAGE_TIGGER_STEP_2 = 2; Context mContext = this; private AlarmManager mAlarm; private PowerManager mPowerManager; final HandlerThread mHandlerThread = new HandlerThread(TAG); private PendingIntent mPendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Init(); final TriggerHandler mTriggerHandler = new TriggerHandler(mHandlerThread.getLooper());// handler 需要在thread启动之后才能进行getlooper mBtSendMsg = (Button)findViewById(R.id.msgbutton); mBtSendMsg.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (bTigger) { bTigger = false; Log.d(TAG, "Press button to Send MSG in IF"); //mTriggerHandler.sendEmptyMessage(MESSAGE_TIGGER_STEP_1); //Message mMessage = mTriggerHandler.obtainMessage(MESSAGE_TIGGER_STEP_1);//需要即时定义Message,如果只定义一个反复发送会报错 final Message mMessage = mTriggerHandler.obtainMessage(MESSAGE_TIGGER_STEP_1); mMessage.setData(extras); mTriggerHandler.sendMessage(mMessage);//如果不final 类型的msg会有错误,如果反复点击需要修改成obtainMessage } else { bTigger = true; Log.d(TAG, "Press button to Send MSG in ELSE"); mTriggerHandler.sendEmptyMessage(MESSAGE_TIGGER_STEP_2); } } }); } private void Init(){ extras.putInt("firstMsgNum", 1); extras.putString("fristMsgName", "FirstMsg"); registerReceiver(); mHandlerThread.start(); mHandlerThread.getLooper(); if (mAlarm == null) { mAlarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); } if (mPowerManager == null) { mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); } } private final class TriggerHandler extends Handler{ public TriggerHandler(Looper looper){ super(looper); } @Override public void handleMessage(Message msg){ Bundle extras = msg.getData(); switch (msg.what) { case MESSAGE_TIGGER_STEP_1: Log.d(TAG, "Get the MESSAGE_TIGGER_STEP_1"); Log.d(TAG, "get the data,num = "+extras.getInt("firstMsgNum")+" name = "+extras.getString("fristMsgName")); break; case MESSAGE_TIGGER_STEP_2: Log.d(TAG, "Get the MESSAGE_TIGGER_STEP_2"); break; default: Log.d(TAG, "Do nothing in default"); break; } } } public void registerReceiver(){//add some action to listern IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_BOOT_COMPLETED); filter.addAction(PENDING_INTENT_ALARM); mContext.registerReceiver(mIntenReceiver, filter); } private BroadcastReceiver mIntenReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context,Intent intent){ String action = intent.getAction(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { Log.d(TAG, "Will Send alarm"); } else if (action.equals(intent.ACTION_SCREEN_ON)) { Log.d(TAG, "Will Send Screen On alarm"); if(mPendingIntent== null){ Intent pendingIntent = new Intent(PENDING_INTENT_ALARM).setPackage(null).setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); mPendingIntent = PendingIntent.getBroadcast(mContext, 0, pendingIntent, 0); mAlarm.setExact(AlarmManager.RTC,System.currentTimeMillis()+alarmTime, mPendingIntent); } } else if (action.equals(intent.ACTION_SCREEN_OFF)) { Log.d(TAG, "Will Send Screen Off alarm"); } else if (action.equals(PENDING_INTENT_ALARM)){ final TriggerHandler mTriggerHandler = new TriggerHandler(mHandlerThread.getLooper()); mTriggerHandler.sendEmptyMessage(MESSAGE_TIGGER_STEP_2); } } };}
程序流程:
初始化->创建thread->创建handler->注册广播接收-> 监听bootcomplete/ScreenOn/ScreenOff->收到亮屏广播->设置Alarm->5秒后发送Intent->handler接收并做相应的处理
知识点
Handler
Intent&PendingIntent
BroadcastReceiver
Alarm
0 0
- [Android]Broadcast,Intent,PendingIntent,Handler例子
- 【Android】pendingIntent和Intent
- Android simplest intent broadcast
- android broadcast intent
- (Android) Broadcast Intent Permissions
- Android - Intent广播(broadcast)
- Android学习--broadcast intent
- android 之 Intent、broadcast
- Android - Intent、PendingIntent、IntentFilter、BroadcastReceiver
- android Intent PendingIntent的区别
- android Intent、pendingIntent机制详解
- Android Broadcast 小例子
- intent, PendingIntent
- Intent PendingIntent
- Android PendingIntent和Intent的区别
- android之Intent、IntentFilter、IntentService、PendingIntent
- Android Intent和PendingIntent的区别
- Android-Intent和PendingIntent的关系
- ecshop二次开发--左侧导航
- 使用spring进行属性注入的实例解析(spring面向切面和控制反转)。
- 从Google学到的厕所文化
- 回车和换行
- Android 日期工具类DateUtil
- [Android]Broadcast,Intent,PendingIntent,Handler例子
- Discuz! X1的目录结构和模板结构
- Git命令家底儿及Git数据通信原理详解
- IOS高级开发~Runtime(一)
- 关于width与padding
- 剑指offer之面试题17合并两个排序的链表
- Top K Frequent Elements
- SQL获取每月最后一天记录
- MathType的这些公式技巧,你绝对没想到!