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

泪湿孤枕 提交于 2019-12-21 04:32:09

问题


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>

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/6402679/launching-an-activity-from-a-broadcast-receiver-that-listens-to-outgoing-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!