My Broadcast receiver get execute even if my appli

2019-08-03 21:38发布

问题:

My broadcast receiver is Still getting execute even if My application is not working.

as an example I am using android.intent.action.NEW_OUTGOING_CALL to check outgoing call and than i stop music and push notification .. but even i close my app and kill all task and after if i call than i get notification of my app.. So how do i manage to work my broadcast when i am using my app. I have crated Service to play music and 2 broadcast receiver file for incoming and outgoing. Help to solve this problem.

Also How can i destroy my app with service running behind If user press exit button. **Update I made edited it and its working now fine..thank you so much you all

here is my code

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />


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

    <receiver android:name="OutgoingCallInterceptor">                          
        <intent-filter android:priority="1">                                  
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  
        </intent-filter>
    </receiver>

    <receiver android:name="IncomingCallInterceptor">                          
        <intent-filter android:priority="1">                                  
            <action android:name="android.intent.action.PHONE_STATE" />  
            <action android:name="android.media.AUDIO_BECOMING_NOISY" />

        </intent-filter>
    </receiver>

Update as you all suggest me i have made receiver into main class file and register it from there but it wont work

public class MainActivity extends Activity {
RemoteViews layout;
int SDK_INT;
 BroadcastReceiver br;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     IntentFilter filter = new IntentFilter("android.media.AUDIO_BECOMING_NOISY");
     this.registerReceiver(br, filter);
     setContentView(R.layout.activity_main);
         SDK_INT = android.os.Build.VERSION.SDK_INT;
        System.out.println(SDK_INT);
        Button start = (Button)findViewById(R.id.play);
        Button stop = (Button)findViewById(R.id.stop); 
        start.setOnClickListener(startListener);
        stop.setOnClickListener(stopListener);
        br = new BroadcastReceiver() {  
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                 if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
context.stopService(new Intent(context, myPlayService.class));
                        Toast.makeText(context, "Headphones disconnected.", Toast.LENGTH_SHORT).show();
                 }
            }  }
        };

@Override   
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(br);
}

回答1:

an example: how a broadcast reciver can be registered and un registered change as per your need "i hope the code is self explanatory"

 private final BroadcastReceiver xyz= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
//ur reciver
 }
  protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

IntentFilter filter = new IntentFilter(ACTION);//ur action
this.registerReceiver(xyz, filter);
}

@Override
protected void onDestroy(){
super.onDestroy();
unregisterReceiver(xyz);
 }


回答2:

You need to declare unregisterReceiver() in onResume() or in onpause() methods. This will solve your problem.



回答3:

First:

use unregisterReceiver() in onPause() and re-register in onStart().

It's always better to have locally registering the receiver if you want to provide the functionality only when your app is up.

Second:

Use service after binding it and don't call unBind while exiting the app will keep your service alive even after your app is down. I guess you are starting the service locally. Start service by binding it.

Hope this will solve your problem.