Reputation: 696
The code that I am presently using is as follows:
My main class
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);
}
}
the CustomPhoneStateListener class
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;
}
}
}
and the Android Manifest file as:
<?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>
By using the above code I am able to get the toast message for the respective actions that I am performing ,but I am not able to give any Intent or by using the Package_Manger I am not able to shift to the next app.
The log_cat message returned when trying to execute the code for invoking the app:
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)
Upvotes: 0
Views: 2397
Reputation: 6663
It looks like the problem is coming from the use of active
declared like this:
public Activity activ=new Activity(){
public void startActivity(Intent i) {}
};
Here you're creating a new Activity without a context. Maybe you should just pass the Activity right into your CustomPhoneStateListener instead of the Context. Or use the context
to get the PackageManager:
PackageManager manager = context.getPackageManager();
Upvotes: 1
Reputation: 24021
For this you need to listen for the Phone State. When you get call on your broadcast receiver then start this service and it will take care rest part of your handling.
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() {
}
}
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Upvotes: 1