gpt4 book ai didi

android - BroadcastReceiver 在我的 Android 应用程序中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:05 25 4
gpt4 key购买 nike

我有一个简单的 Android 应用程序,它包含一个 broadcastreceiver 类作为 MainActivity 中的内部类。代码如下。

private class MyReceiver extends BroadcastReceiver {

private Intent receivedIntent;

@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyTag","onReceive function....!!!!!");

receivedIntent = intent;
}
}

根据一些标准教程,我可以在 list 文件中为接收器设置一个静态条目。

<receiver
android:name = ".MyReceiver"
android:enabled = "true">
<intent-filter>
<action android:name = "android.intent.action.ACTION_SCREEN_OFF"/>
<action android:name = "android.intent.action.ACTION_SCREEN_ON"/>
</intent-filter>
</receiver>

但在我将以下行放入 MainActivity 之前它不起作用。

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);

DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);

即使在 list 文件中输入之后,我是否还需要执行 registerReceiver?感谢您的帮助。

但是在关闭我的应用程序时我遇到了另一个问题。这就是 ADM 显示的错误。

11-02 23:01:10.178: E/ActivityThread(11121): Activity com.sony.datamoduledesignproject.MainActivity has leaked IntentReceiver com.sony.datamoduledesignproject.MainActivity$DataWriteReceiver@2911ab22 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-02 23:01:10.178: E/ActivityThread(11121): android.app.IntentReceiverLeaked: Activity com.sony.datamoduledesignproject.MainActivity has leaked IntentReceiver com.sony.datamoduledesignproject.MainActivity$DataWriteReceiver@2911ab22 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:970)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:771)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:2014)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1994)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1988)
11-02 23:01:10.178: E/ActivityThread(11121): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
11-02 23:01:10.178: E/ActivityThread(11121): at com.sony.datamoduledesignproject.MainActivity.intentGenerator(MainActivity.java:93)
11-02 23:01:10.178: E/ActivityThread(11121): at com.sony.datamoduledesignproject.MainActivity.onCreate(MainActivity.java:78)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.Activity.performCreate(Activity.java:6374)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.access$900(ActivityThread.java:181)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
11-02 23:01:10.178: E/ActivityThread(11121): at android.os.Handler.dispatchMessage(Handler.java:102)
11-02 23:01:10.178: E/ActivityThread(11121): at android.os.Looper.loop(Looper.java:145)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.main(ActivityThread.java:6145)
11-02 23:01:10.178: E/ActivityThread(11121): at java.lang.reflect.Method.invoke(Native Method)
11-02 23:01:10.178: E/ActivityThread(11121): at java.lang.reflect.Method.invoke(Method.java:372)
11-02 23:01:10.178: E/ActivityThread(11121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-02 23:01:10.178: E/ActivityThread(11121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这里提到的 MainActivity 中的错误是在下面给出的 registerReceiver() 调用上,

DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);

总结是 MainActivity 泄露了 IntentReceiver,我需要取消注册 Receiver() 吗?

请你帮帮我。

最佳答案

很多人问过这种问题,我刚才搜索过这种问题,但是没有一个答案向我们展示关于这个问题的官方答案。所以在这里

http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON

enter image description here

http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF

enter image description here

Remember:The Android official documentation will give you better basic understanding of the Android,not sometimes,but ALMOST everytime.

关于android - BroadcastReceiver 在我的 Android 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33478550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com