android – 用于Screen On / Off的BroadcastReceiver无法正常工作

前端之家收集整理的这篇文章主要介绍了android – 用于Screen On / Off的BroadcastReceiver无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用BroadcastReceiver,但它不工作,请帮我解决这个问题.
MyReceiver. java
  1. package com.example.broadcast_receiver;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.util.Log;
  7.  
  8. public class MyReceiver extends BroadcastReceiver {
  9.  
  10. @Override
  11. public void onReceive(Context context,Intent intent) {
  12. // TODO Auto-generated method stub
  13. Log.i("[BroadcastReceiver]","MyReceiver");
  14.  
  15. if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
  16. Log.i("[BroadcastReceiver]","Screen ON");
  17. }
  18. else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
  19. Log.i("[BroadcastReceiver]","Screen OFF");
  20. }
  21. }
  22. }

AndroidManifest.xml中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.broadcast_receiver"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="9"
  9. android:targetSdkVersion="16" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16.  
  17. <receiver android:name=".MyReceiver"
  18. android:enabled="true"
  19. android:exported="false">
  20. <intent-filter>
  21. <action android:name="android.intent.action.SCREEN_ON"/>
  22. <action android:name="android.intent.action.SCREEN_OFF"/>
  23. </intent-filter>
  24. </receiver>
  25.  
  26. <activity
  27. android:name="com.example.broadcast_receiver.MainActivity"
  28. android:label="@string/app_name" >
  29. <intent-filter>
  30. <action android:name="android.intent.action.MAIN" />
  31. <category android:name="android.intent.category.LAUNCHER" />
  32. </intent-filter>
  33. </activity>
  34. </application>
  35.  
  36. </manifest>

BroadcastReceiver无法正常工作且没有制作任何日志,请帮我解决这个问题.

解决方法

嘿尝试使用动态调用广播,我尝试了它将乖乖工作…
  1. public class MainActivity extends Activity {
  2.  
  3. //Create broadcast object
  4. BroadcastReceiver mybroadcast = new BroadcastReceiver() {
  5. //When Event is published,onReceive method is called
  6. @Override
  7. public void onReceive(Context context,Intent intent) {
  8. // TODO Auto-generated method stub
  9. Log.i("[BroadcastReceiver]","MyReceiver");
  10.  
  11. if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  12. Log.i("[BroadcastReceiver]","Screen ON");
  13. }
  14. else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  15. Log.i("[BroadcastReceiver]","Screen OFF");
  16. }
  17.  
  18. }
  19. };
  20.  
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. registerReceiver(mybroadcast,new IntentFilter(Intent.ACTION_SCREEN_ON));
  27. registerReceiver(mybroadcast,new IntentFilter(Intent.ACTION_SCREEN_OFF));
  28. }
  29. }

猜你在找的Android相关文章