嗨,我正在BroadCastReciver上工作。定義BroadCastReciver有兩種方法。第一種是使用Java代碼,第二種是在AndroidManifest.xml中使用定義。在我的代碼中,第二個是不正常工作。請告訴我哪裏出錯了。爲什麼BroadcastReceiver不捕捉Action?
public class HotelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String dActionName = intent.getAction();
Log.i("My Rceiver ", intent.getAction());
if (dActionName.equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "SCREEN ON", Toast.LENGTH_SHORT).show();
} else if (dActionName.equals(Intent.ACTION_SCREEN_OFF)) {
}
}
}
AndroidManifest.xml中
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hotelsecurity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10"
android:maxSdkVersion="15" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
</manifest>
*第二個是不正常工作*意味着它的工作和u沒有得到預期的結果? – 2013-03-27 06:53:57
ρяσѕρєяK邑我沒有得到預期的結果檢查靜態註冊http://www.jiahaoliuliu.com/2011/09/android-registering-broadcast-receiver.html – 143 2013-03-27 07:00:07