如何在通话结束时调用/启动应用程序?

时间:2011-11-07 08:26:53

标签: android

我目前使用的代码如下:

我的主要课程

  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 Manifest文件:

 <?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>

通过使用上面的代码,我能够获取我正在执行的相应操作的toast消息,但我无法提供任何 Intent 或使用 Package_Manger < / strong>我无法转到下一个应用程序。

尝试执行调用app的代码时返回的log_cat消息:

 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)

2 个答案:

答案 0 :(得分:1)

为此你需要听取电话状态。当您在广播接收器上接听电话时,请启动此服务,并将部署您的处理工作。

public class MyPhoneStateListener extends Service{

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
        StateListener phoneStateListener = new StateListener();
        TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }catch(Exception e){ }

}

class StateListener extends PhoneStateListener{
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Here call your function to invoke another app.
                startAnotherApp(String pkgName);
                break;
        }
    }
};

private void startAnotherApp(String pkgName){
  Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  if (null != intent) {
  try {
         startActivity(intent);
  } catch(Exception e) {  }
}

}

@Override
public void onDestroy() {

}

}

许可:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

答案 1 :(得分:1)

看起来问题来自使用active这样声明:

    public  Activity activ=new Activity(){
        public void startActivity(Intent i) {}
    };

在这里,您将创建一个没有上下文的新活动。也许您应该将Activity直接传递给CustomPhoneStateListener而不是Context。或者使用context获取PackageManager:

PackageManager manager = context.getPackageManager();