BroadcastReceiver 不起作用,没有通知
BroadcastReceiver does not work, no notification
我尝试做一个简单的应用程序(或者,更具体地说,是一个 Receiver):
当用户按下 "Camera button" 或 "Dealer button" - 应用会发出通知。
没有错误,但应用程序无法运行。
我尝试添加权限,但还是不行。
MyDownloadBroadcastReceiver.java:
package elyahsiv.raisenotificationwhendownloadfile;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
public class MyDownloadBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive (Context context, Intent intent) {
Log.d("NOTES:", intent.getAction());
if (intent.getAction() == "android.intent.action.PACKAGE_ADDED")
showNotification(context);
else if (intent.getAction() == "android.intent.action.CAMERA_BUTTON")
showNotification(context);
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.stat_notify_error)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="elyahsiv.raisenotificationwhendownloadfile">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyDownloadBroadcastReceiver" android:enabled="true" android:exported="false">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<action android:name="android.intent.action.CALL_BUTTON"></action>
</intent-filter>
</receiver>
</application>
</manifest>
必须在应用程序标记之前声明权限。
您必须拥有 android.permission.CAMERA 甚至更多。
在 Intent 过滤器中你必须有
<data android:scheme="package" />
考虑将接收器分成三个不同的接收器。
还有
Due to a broadcast behavior change since Android 3.1, your app must be started before it can receive the app installation/removal intents. See kabuko's answer in this thread.
检查此 answer。
尝试在 activity 或创建服务时注册您的接收器。
您还应该考虑以下几点:
Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
例如:
MyDownloadBroadcastReceiver receiver = new MyDownloadBroadcastReceiver();
IntentFilter receiverFilterCameraButton = new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver( receiver, receiverFilterCameraButton );
我尝试做一个简单的应用程序(或者,更具体地说,是一个 Receiver):
当用户按下 "Camera button" 或 "Dealer button" - 应用会发出通知。
没有错误,但应用程序无法运行。
我尝试添加权限,但还是不行。
MyDownloadBroadcastReceiver.java:
package elyahsiv.raisenotificationwhendownloadfile;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
public class MyDownloadBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive (Context context, Intent intent) {
Log.d("NOTES:", intent.getAction());
if (intent.getAction() == "android.intent.action.PACKAGE_ADDED")
showNotification(context);
else if (intent.getAction() == "android.intent.action.CAMERA_BUTTON")
showNotification(context);
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.stat_notify_error)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="elyahsiv.raisenotificationwhendownloadfile">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyDownloadBroadcastReceiver" android:enabled="true" android:exported="false">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<action android:name="android.intent.action.CALL_BUTTON"></action>
</intent-filter>
</receiver>
</application>
</manifest>
必须在应用程序标记之前声明权限。 您必须拥有 android.permission.CAMERA 甚至更多。 在 Intent 过滤器中你必须有
<data android:scheme="package" />
考虑将接收器分成三个不同的接收器。 还有
Due to a broadcast behavior change since Android 3.1, your app must be started before it can receive the app installation/removal intents. See kabuko's answer in this thread.
检查此 answer。
尝试在 activity 或创建服务时注册您的接收器。 您还应该考虑以下几点:
Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
例如:
MyDownloadBroadcastReceiver receiver = new MyDownloadBroadcastReceiver();
IntentFilter receiverFilterCameraButton = new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver( receiver, receiverFilterCameraButton );