BroadcastReceiver不起作用,没有通知

时间:2017-09-28 10:53:35

标签: android broadcastreceiver

我试过做一个简单的应用程序(或者更具体地说是接收器):
当用户按下"相机按钮"或者"经销商按钮" - 应用程序发出通知。

没有错误,但应用程序无法正常工作。

我尝试添加权限,但它仍然无效。

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>

1 个答案:

答案 0 :(得分:0)

必须在应用程序标记之前声明权限。 你必须有android.permission.CAMERA或许更多。 在意图过滤器中你必须有

<data android:scheme="package" />

考虑将接收器拆分为三个不同的接收器。 还

  

由于Android 3.1以来的广播行为更改,您的应用必须先启动,然后才能收到应用安装/删除意图。请参阅kabuko的回答in this thread

选中answer

尝试在创建活动或服务时注册您的接收者。 您还应该考虑以下事项:

  

针对Android 8.0或更高版本的应用无法再在其清单中为隐式广播注册广播接收器。

例如:

MyDownloadBroadcastReceiver receiver = new MyDownloadBroadcastReceiver(); 
IntentFilter receiverFilterCameraButton = new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver( receiver, receiverFilterCameraButton );