[英]Why isn't my Screen On/Off BroadcastReceiver working?
Here is my Receiver file.这是我的接收器文件。 Which I feel doesn't have any kind of problem.我觉得没有任何问题。 I just prints the type of intent which is passed.我只是打印传递的意图类型。 According to me onReceive is never called.据我说 onReceive 从来没有被调用过。 I guess there is a problem with my manifest itself?我猜我的清单本身有问题? Or any other ideas?还是有其他想法? The Receiver is not linked to any anyother code in the application.接收器未链接到应用程序中的任何其他代码。
package sharukh.locky;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastReceiver extends BroadcastReceiver
{
public BroadCastReceiver()
{
}
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("rece", intent.getAction());
}
}
My Manifest also looks good, but I can't figure out why it isn't working.我的清单看起来也不错,但我不知道为什么它不起作用。
<?xml version="1.0" encoding="utf-8"?>
<manifest package="sharukh.locky"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAGufoN9huWkmw8nTskk2aaFHW1gXn1Z7Q"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<!-- Activities -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Tutorial"/>
<activity android:name=".TutorialSelectApp"/>
<activity
android:name=".LockScreen"
android:label="@string/title_activity_lock_screen"
android:theme="@style/AppTheme.NoActionBar"/>
<!-- Services -->
<service
android:name=".LockyService"
android:enabled="true"
android:exported="true"/>
<receiver
android:name=".BroadCastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.HEADSET_PLUG"/>
</intent-filter>
</receiver>
</application>
</manifest>
I haven't dynamically added/registered/unregistered any receivers.我没有动态添加/注册/取消注册任何接收器。 I would require some help here.我需要一些帮助。
You can do this by trying dynamic calling of broadcast like below您可以通过尝试动态调用广播来做到这一点,如下所示
public class YourActivity extends Activity {
//Create broadcast object
BroadcastReceiver mBroadcast = new BroadcastReceiver() {
//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("[BroadcastReceiver]", "Screen ON");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("[BroadcastReceiver]", "Screen OFF");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.