Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';广播接收机不工作吗?我在这个网站上以及其他网站上都尝试过很多解决方案,但它不是';不起作用。帮助我_Java_Android_Xml_Broadcastreceiver - Fatal编程技术网

Java 为什么不是';广播接收机不工作吗?我在这个网站上以及其他网站上都尝试过很多解决方案,但它不是';不起作用。帮助我

Java 为什么不是';广播接收机不工作吗?我在这个网站上以及其他网站上都尝试过很多解决方案,但它不是';不起作用。帮助我,java,android,xml,broadcastreceiver,Java,Android,Xml,Broadcastreceiver,这是我在广播接收器类中的代码 package com.example.manjit.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class MyReceiver extends

这是我在广播接收器类中的代码

package com.example.manjit.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "working!!", Toast.LENGTH_SHORT).show();
    }
}
清单文件包含以下代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.manjit.myapplication">
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>


我已经尝试了很多动作的代码,比如
action-android:name=“android.intent.action.HEADSET\u PLUG”
,但是没有显示祝酒词。请帮帮我。我试过在Lolipop上运行

尝试添加两个操作:

<receiver android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
</receiver>

什么不起作用?你在哪个操作系统中尝试过?我在Android 5.1.1上尝试过。没有显示toast。你需要向你的应用程序添加一个
活动
,安装后至少运行一次,以使你的应用程序脱离停止状态。你的听筒在那之前不能工作。此外,您不能在清单注册接收器中接收
耳机插头
广播。对于Android 8及更新版本上的大多数隐式广播,您不能在清单中注册接收器。
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == Intent.ACTION_POWER_CONNECTED) {
            Toast.makeText(context, "ACTION_POWER_CONNECTED", Toast.LENGTH_SHORT).show();
        } else if (intent.getAction() == Intent.ACTION_POWER_DISCONNECTED) {
            Toast.makeText(context, "ACTION_POWER_DISCONNECTED", Toast.LENGTH_SHORT).show();
        }
    }
}