onReceive BroadcastReceiver 从未调用过明确的意图 Android android Pie
onReceive BroadcastReceiver never called for explicit intent Android android Pie
我已经在我的清单中注册了一个静态接收器,但我不知道为什么我的 onReceive 从未被调用过。我已经搜索了很多并检查了几个解决方案,但是 none 它们都有效,因为它们都是相同的。
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadcast(new Intent("com.example.androidsinglebcreceiver.show_toast"));
}
});
}
@Override
protected void onResume() {
super.onResume();
}
}
广播接收器:
public class Receiver extends BroadcastReceiver {
private final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "INTENT RECEIVED");
Toast.makeText(context, "INTENT RECEIVED by Receiver", Toast.LENGTH_LONG).show();
}
}
清单:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.androidsinglebcreceiver.show_toast" >
</action>
</intent-filter>
</receiver>
</application>
试试这样发送你的广播。
sendBroadcast(new Intent(MainActivity.this,Receiver.class).setAction("com.example.androidsinglebcreceiver.show_toast"));
您需要像这样发送广播并在清单中提供完整的包名称注册。
Intent sendBcIntent = new Intent(MainActivity.this, Receiver.class);
sendBcIntent.setAction("com.example.androidsinglebcreceiver.show_toast");
sendBroadcast(sendBcIntent);
我已经在我的清单中注册了一个静态接收器,但我不知道为什么我的 onReceive 从未被调用过。我已经搜索了很多并检查了几个解决方案,但是 none 它们都有效,因为它们都是相同的。
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadcast(new Intent("com.example.androidsinglebcreceiver.show_toast"));
}
});
}
@Override
protected void onResume() {
super.onResume();
}
}
广播接收器:
public class Receiver extends BroadcastReceiver {
private final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "INTENT RECEIVED");
Toast.makeText(context, "INTENT RECEIVED by Receiver", Toast.LENGTH_LONG).show();
}
}
清单:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.androidsinglebcreceiver.show_toast" >
</action>
</intent-filter>
</receiver>
</application>
试试这样发送你的广播。
sendBroadcast(new Intent(MainActivity.this,Receiver.class).setAction("com.example.androidsinglebcreceiver.show_toast"));
您需要像这样发送广播并在清单中提供完整的包名称注册。
Intent sendBcIntent = new Intent(MainActivity.this, Receiver.class);
sendBcIntent.setAction("com.example.androidsinglebcreceiver.show_toast");
sendBroadcast(sendBcIntent);