簡體   English   中英

盡管發送廣播正在工作,但接收器廣播不起作用

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

Android Studio:3.0,Android Marshmallow

發送廣播:

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");
    }
}

接收廣播

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

 <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>

以上發送廣播運行正常。 我什至使用日志檢查過它。 但是接收廣播沒有運行。 沒有顯示吐司/日志消息。 是什么原因?

可以在代碼中可以獲得 Context 實例的任何部分中使用廣播。 但是,要接收,您必須收聽特定的廣播,這可以使用簡單的 IntentFilter 完成,然后您可以使用給定的意圖過濾器注冊 BroadcastReceiver。 下面顯示了一個示例實現;

要發送的簡單廣播,我使用 Context 對象將其發送到此處(Fragments 和其他一些類需要):

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

下面還詳細闡述了一種簡單的接收方式:

首先,創建一個 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
            }
        }
    };

接下來,使用 IntentFilter 注冊一個 BroadcastReceiver,一個好的地方是在你的可執行類(Fragment、Activity、Service 或 Applcation 類)的 onCreate 或 onResume 方法中:

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

IntentFilter 是什么讓你在你的應用程序中獲取適當的調用,因為 android 在設備上發送了很多意圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM