,我目前使用的代碼如下:如何在通話結束時調用/啓動應用程序?
我主類
public class Index extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
的CustomPhoneStateListener類
public class CustomPhoneStateListener extends PhoneStateListener {
public Activity activ=new Activity(){
public void startActivity(Intent i) {}
};
Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "call has ended", Toast.LENGTH_SHORT).show();
// The process to transfer to the next application.
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = activ.getPackageManager();
i = manager.getLaunchIntentForPackage("com.timetracker.app");// package name for my new app
i.addCategory(Intent.CATEGORY_LAUNCHER);
activ.startActivity(i);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "Phone call has taken", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
Toast.makeText(context, "Phone is ringing", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
和Android清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.call"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".Index" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.view.InputMethod" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
通過使用上面的代碼我能夠得到我正在執行相應的動作敬酒的消息,但我不能給任何意圖或使用Package_Manger我不能夠轉移到下一個應用程序。
D/AndroidRuntime(312): Shutting down VM
: W/dalvikvm(312): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: main
E/AndroidRuntime(312): java.lang.NullPointerException
E/AndroidRuntime(312): at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:85)
E/AndroidRuntime(312): at com.android.call.CustomPhoneStateListener.onCallStateChanged(CustomPhoneStateListener.java:39)
E/AndroidRuntime(312): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
E/AndroidRuntime(312): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(312): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(312): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(312): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(312): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(312): at dalvik.system.NativeStart.main(Native Method)
謝謝..現在它正在工作.. – DAS