Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

How to detect Incoming call in background

I am trying to create on application. I need to detect if i get any call then get number but this all process should happen in background.
Background working is latter part but now i am trying start my application but it's crashing.
Can you also tell me how to call broadcastreciver in backgound? Here is my MainActivity.java

public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstancestate)
{
    super.onCreate(savedInstancestate);
    setContentView(R.layout.activity_main);

}
public void broadcastIntent(View view)
{
      Intent intent = new Intent();
    intent.setAction("com.app.callrecord.MyBroadcastReceiver");
    sendBroadcast(intent);
} 
}

Broadcastreceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
   Bundle bundle = intent.getExtras();
   if (bundle == null)
      return;
   String phoneNumber = null;

   // Incoming call
   String state = bundle.getString(TelephonyManager.EXTRA_STATE);
   if ((state != null) &&  (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
       phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);  
      // callToast(phoneNumber);

   }
   // Outgoing call
   else if (state == null) {        
       Intent i = new Intent(context,RecordHistory.class);  
       intent.putExtra("phonenumber", phoneNumber);
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
       context.startActivity(i);  
      // Here: do something with the number
   }  
 }
}

Here is my manifest file

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="com.app.callrecord.MainActivity">
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"></action>
    <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RecordHistory"></activity>
</application>

And here is my exception log

 02-18 13:36:17.240: E/AndroidRuntime(9397): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.callrecord/com.app.callrecord.MyBroadCastReciever}: java.lang.ClassNotFoundException: com.app.callrecord.MyBroadCastReciever
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2278)
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
02-18 13:36:17.240: E/AndroidRuntime(9397):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)

Give me any reference.

Upvotes: 0

Views: 2927

Answers (2)

Tomik
Tomik

Reputation: 23977

You have a mistake in your manifest. The following line refers wrongly to the activity:

<receiver android:name="com.app.callrecord.MainActivity">

It should refer to the receiver:

<receiver android:name="SOME_PACKAGE_HERE.MyBroadcastReceiver">

Edit:

If the broadcast receiver doesn't get called, you probably don't have permissions to detect the incoming and outgoing phone calls. You need android.permission.READ_PHONE_STATE and android.permission.PROCESS_OUTGOING_CALLS permissions in your manifest.

Upvotes: 2

Anup Cowkur
Anup Cowkur

Reputation: 20563

I think the problem is that you have declared the name wrong in the manifest. You have declared it as

<receiver android:name="com.app.callrecord.MainActivity">

whereas it should be

<receiver android:name="com.app.callrecord.MyBroadCastReciever">

Upvotes: 1

Related Questions