Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

launching an activity from a broadcast receiver that listens to outgoing call

I am trying to launch an activity from a broadcast receiver that listens to outgoing call which is 5556. The problem is, the activity is not being launched but the dial inbuilt activity is being called, I have changed the priority of the intent to 100 but to no avail. How do I get the activity to launch at dial instead of the inbuilt calling activity? Here is the code:

package com.messageHider;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class launchReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        String compare_num="5556";
        if(number.equals(compare_num))
        {
            Intent myintent=new Intent(context,messageHider.class);
            myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myintent);
            abortBroadcast();
        }
    }

}

Manifest file:

<receiver android:name=".launchReceiver">
    <intent-filter android:priority="0">
        <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter>
</receiver>
like image 996
John Avatar asked Jun 19 '11 13:06

John


People also ask

Which activity is used to give information about broadcast receiver?

registerReceiver() method. The implementing class for a receiver extends the BroadcastReceiver class. If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.

What is a broadcast receiver how system broadcast will be received by applications?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What is used to listen for broadcast intents?

Broadcast Receivers are used to listen for Broadcast Intents. To enable a Broadcast Receiver, it needs to be registered, either in code or within the application manifest.


2 Answers

In your manifest you require one permission (according to the docs):

You must hold the PROCESS_OUTGOING_CALLS permission to receive this Intent.

And that is

"android.permission.PROCESS_OUTGOING_CALLS"

Also straight from the docs:

Any BroadcastReceiver receiving this Intent must not abort the broadcast.

And here is the link to the page where I got the info:

http://developer.android.com/reference/android/content/Intent.html

like image 89
Reed Avatar answered Nov 10 '22 08:11

Reed


Instead of aborting broadcast with abortBroadcast(); use setResultData(null) to end the call after launching your activity.

like image 24
Michael Nwanna Avatar answered Nov 10 '22 07:11

Michael Nwanna