向系统注册回调(Callback)函数的方法--获取电话状态更新和短信接收通知
来源:互联网 发布:mac化妆刷 编辑:程序博客网 时间:2024/06/05 08:54
下面的实例展示了向系统注册回调(Callback)函数的方法;实例中当电话状态发生改变时,应用程序将会得到系统的通知,Callback函数得以调用。相信这将是我们手机应用中经常需要用到的。
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.Phone;
import android.telephony.PhoneStateIntentReceiver;
import android.util.Log;
public class androidtest extends Activity {
/** Used to recognize Messages from the
* myPhoneStateChangedHandler. */
final int PHONECALLSTATE_RECONGNIZE_ID = 0x539;
/** Will notify us on changes to the PhoneState*/
PhoneStateIntentReceiver myPsir = null;
/** This Handler will react on the messages the
* we made our PhoneStateIntentReceiver myPsir
* notify us on. */
Handler myPhoneStateChangedHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// Recognize the Message by its what-ID
if(msg.what == PHONECALLSTATE_RECONGNIZE_ID){
/* Our PhoneStateIntentReceiver myPsir
* now contains some recent data, we can grab. */
Phone.State myState = myPsir.getPhoneState();
// Put the Info to the logger for debugging
Log.d("PhoneCallStateNotified", myState.toString());
if(myState == Phone.State.RINGING){
// Celebrate =D
}
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
// Set some simple layout
super.onCreate(icicle);
setContentView(R.layout.main);
/* Create a new PhoneStateIntentReceiver
* that will pass messages to the handler h
* as it receives Intents we make it notify
* us below*/
this.myPsir = new PhoneStateIntentReceiver(this, myPhoneStateChangedHandler);
/* As we want to get notified on changes
* to the Phones-State we tell our
* PhoneStateIntentReceiver myPsir,
* that we wan to get notified with the ID
* (PHONECALLSTATE_RECONGNIZE_ID) we pass to him
*/
this.myPsir.notifyPhoneCallState(PHONECALLSTATE_RECONGNIZE_ID);
/* Register the Intent with the system. */
this.myPsir.registerIntent();
}
}
- 向系统注册回调(Callback)函数的方法--获取电话状态更新和短信接收通知
- Android获取系统启动器、电话、短信和相机包名
- 短信诈骗和“响一声”恶意电话公开(持续更新)
- iOS系统邮件 短信 电话 网页的调用方法
- android 4.4 动态注册+静态注册电话短信广播后收到两次onReceive回调的问题
- iOS关于系统短信和电话的调用
- TI C66x DSP平台向APP提供callBack函数注册
- JavaScript的回调(CallBack)方法
- 获取国外电话途径的方法(电话号码可以注册google账号)
- Android 截取指定号码的短信 并且不让系统获取短信通知用户
- Android 截取指定号码的短信,并且不让系统获取短信通知用户
- 在线接收短信和电话验证平台 收集
- android调用系统电话和短信
- 函数回调(callback)
- iOS程序的五种状态,AppDelegate中的回调方法和通知
- Android 电话状态的监听和回调
- iOS 短信与电话事件的获取
- iOS 短信与电话事件的获取
- 网页菜单详解:基本原理
- 关于重定向的问题
- 注册SMS接收回调函数实例
- 用ASP.NET和Ajax搭建了一个框架
- 回家的感觉真好
- 向系统注册回调(Callback)函数的方法--获取电话状态更新和短信接收通知
- Symbian OS控件开发之对话框
- start
- WINCE C#读写INI文件
- oracle表空间还原(数据库还原)
- linux C 字符集转换,UTF-8,GB2312
- 章子怡对婚姻的憧憬:一生一世!——丹比奴
- 微软将用Midori结束Windows时代(摘)
- 【转】猪买房的故事