BroadcastReceiver 无法接收 Intent

BroadcastReceiver is not able to receive the Intent

我想编写一个 BroadcastReceiver 来接收应用程序安装操作。但它失败了,所以我测试我的接收器是否正常。所以定制了一个intent,它也归档了。下面是我的代码。请帮我改正。 public class MyInstallReceiver 扩展 BroadcastReceiver { // public MyInstallReceiver() { // }

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
    Log.d("receiver", "Intent Detected");
    if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
        String packageName = intent.getDataString ();
        //System.out.println ("installed:" + packageName + "package name of the program");
        Log.d("receiver","installed:" + packageName + "package name of the program");
    }
}
}

自定义意图

public void installAPK(View v){
   startActivity(intent);
    Intent intent = new Intent();
    intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
    sendBroadcast(intent);
    Log.d("receiver", "Intent sent");
}

Manifest.xml

       <receiver
        android:name=".MyInstallReceiver"
        android:enabled="true"
        android:exported="true" >
        <Intent-filter>
            <action   android:name = "android.intent.action.PACKAGE_ADDED"/>
            <action   android:name = "android.intent.action.PACKAGE_REMOVED"/>

            <action android:name="com.tutorialspoint.CUSTOM_INTENT">
            </action>
            <Data   android:scheme = "package"   />
        </Intent-filter>
    </receiver>

enter code here

一切看起来都不错,除了清单中的错字。应该是 <intent-filter> 而不是 <Intent-filter>

我不知道你的清单中的拼写是否正确,但这段代码肯定工作得很好:

<receiver android:name=".MyInstallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package"/>
        </intent-filter>
</receiver>

每个应用程序 install/uninstall 都会触发此接收器。