BroadcastReceiver class 不是 运行

BroadcastReceiver class not running

我写了一个 BroadcastReceiver class 用于在断开充电器时接收广播意图,但当我断开充电器时它不显示 Toast。 Logcat 也不显示任何内容。我通过 wifi 使用 ADB。

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

public class My extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Toast.makeText(arg0, "hello", Toast.LENGTH_LONG).show();
    }

}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sumitaich.sunsick"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/hacker"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
      <receiver
        android:name=".My">

         <intent-filter>
            <action android:name="com.sumitaich.sunsick.POWER_CONNECTED" />
       </intent-filter>

</receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="oauth" android:host="t4jsample"/>
        </intent-filter>

    </activity>


    <activity
        android:name=".Detail"
        android:label="@string/title_activity_detail"
        android:parentActivityName="com.sumitaich.sunsick.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.sumitaich.sunsick.MainActivity" />
    </activity>


</application>

将您在 android 清单中的操作更改为:

 <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
 <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />