为什么我的 Screen On/Off BroadcastReceiver 不工作?
Why isn't my Screen On/Off BroadcastReceiver working?
这是我的 Receiver 文件。我觉得这没有任何问题。我只是打印传递的意图类型。根据我的说法,永远不会调用 onReceive。我想我的清单本身有问题吗?或者有什么其他的想法吗?接收器未链接到应用程序中的任何其他代码。
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());
}
}
我的清单看起来也不错,但我不明白为什么它不起作用。
<?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>
我没有动态 added/registered/unregistered 任何接收者。我需要一些帮助。
您可以通过尝试动态调用广播来做到这一点,如下所示
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));
}
}
这是我的 Receiver 文件。我觉得这没有任何问题。我只是打印传递的意图类型。根据我的说法,永远不会调用 onReceive。我想我的清单本身有问题吗?或者有什么其他的想法吗?接收器未链接到应用程序中的任何其他代码。
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());
}
}
我的清单看起来也不错,但我不明白为什么它不起作用。
<?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>
我没有动态 added/registered/unregistered 任何接收者。我需要一些帮助。
您可以通过尝试动态调用广播来做到这一点,如下所示
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));
}
}