嗨,我正在做BroadCastReciver。定义BroadCastReciver.First有两种方式,一种是使用Java代码定义,另一种是使用AndroidManifest.xml定义。在我的代码中,第二个是don't work properly.please tell我哪里出错了。
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>发布于 2013-03-27 15:19:44
我认为你的接收者应该阅读
<receiver
android:name="HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>没有点“。
发布于 2013-03-27 15:59:17
只需在onCreate()中使用此代码即可实现您的活动,
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
HotelReceiver mReceiver = new HotelReceiver(this);
registerReceiver(mReceiver, filter);https://stackoverflow.com/questions/15653227
复制相似问题