简体   繁体   English

尽管发送广播正在工作,但接收器广播不起作用

[英]Receiver Broadcast not Working although send Broadcast is working

Android Studio:3.0,Android Marshmallow Android Studio:3.0,Android Marshmallow

SendBroadcast:发送广播:

package com.example.android.sendbroadcast;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onsendbroadcast(View view){
        Log.v("Inside","OnCLICK");
        Intent intent=new Intent();
        Log.v("Intent","Created");
        intent.setAction("com.example.android.sendbroadcast");
        Log.v("Action","Set");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
        Log.v("Broadcast","Sent");
    }
}

Receive Broadcast接收广播

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

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver(){

    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("Inside","Reciever");
        Toast.makeText(context,"Broadcast Recieved",Toast.LENGTH_LONG).show();
    }
}

Android Manifest.xml Android Manifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.android.sendbroadcast"></action>

        </intent-filter>

    </receiver>
</application>

The above send broadcast is running properly.以上发送广播运行正常。 I even checked it using log.我什至使用日志检查过它。 But the receive broadcast is not running.但是接收广播没有运行。 No toast/log message is being shown.没有显示吐司/日志消息。 What is the Reason?是什么原因?

A broadcast can be used in any part of your code where you can get a Context instance.可以在代码中可以获得 Context 实例的任何部分中使用广播。 However, to receive, you have to listen for a particular broadcast, which can be done with a simple IntentFilter and then you can register a BroadcastReceiver with the given intent filter.但是,要接收,您必须收听特定的广播,这可以使用简单的 IntentFilter 完成,然后您可以使用给定的意图过滤器注册 BroadcastReceiver。 An example implementation is shown below;下面显示了一个示例实现;

A simple broadcast to be sent, I'm using a Context object to send it here (Fragments and some other classes require that):要发送的简单广播,我使用 Context 对象将其发送到此处(Fragments 和其他一些类需要):

Intent in = new Intent(Constants.NETWORK_CHANGE);
in.putExtra(Constants.NETWORK_STATE, Constants.DISCONNECTED);
context.sendBroadcast(in);

A simple way to receive is also elaborated below:下面还详细阐述了一种简单的接收方式:

First, create a BroadcastReceiver instance首先,创建一个 BroadcastReceiver 实例

private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getStringExtra(Constants.NETWORK_STATE).equals(Constants.DISCONNECTED)) {
                //do something here
            }
        }
    };

Next, register a the BroadcastReceiver with an IntentFilter, a good place is in the onCreate or the onResume method of your executable class (Fragment, Activity, Service or Applcation class):接下来,使用 IntentFilter 注册一个 BroadcastReceiver,一个好的地方是在你的可执行类(Fragment、Activity、Service 或 Applcation 类)的 onCreate 或 onResume 方法中:

@Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, new IntentFilter(Constants.NETWORK_CHANGE));
    }

The IntentFilter is what makes you grab the appropriate call in your aplication, since android sends a lot of intents on the device. IntentFilter 是什么让你在你的应用程序中获取适当的调用,因为 android 在设备上发送了很多意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM