为什么我的BroadcastReceiver不起作用?
我在学习使用Android BroadcasReceiver。 我写了这段代码,但它不工作...我尝试了例如改变时间等... 这是什么错误?为什么我的BroadcastReceiver不起作用?
我在清单补充说:
<receiver android:name="com.example.broadcastreceiverspike.Broadcast" >
<intent-filter android:priority="100">
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
<action android:name="android.intent.action.ACTION_SCREEN_OFF" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
我简单BroadcasReceiver:
public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BROADCAST", "It worked");
Toast.makeText(context, "BROADCAST", Toast.LENGTH_LONG).show();
}
}
我的主要活动(默认的主活动)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我解决了!
“与其他广泛流传的意图不同,对于Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON,您无法在Android清单中声明它们!我不知道确切原因,但他们必须在IntentFilter的在Java代码中”
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
清单登记
<receiver android:name=".Broadcast" >
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
主要活动类
公共类MainActivity扩展活动{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new Broadcast();
registerReceiver(mReceiver, filter);
}
}
我的广播接收机
公共类广播延伸的BroadcastReceiver {
public static String TAG = "BROADCAST";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED))
{
Log.d(TAG, "BROADCAST Cambio di orario");
Toast.makeText(context, "BROADCAST Cambio di orario", Toast.LENGTH_LONG).show();
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
Log.d(TAG, "BROADCAST Screen OFF");
Toast.makeText(context, "Screen OFF", Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
Log.d(TAG, "BROADCAST Screen ON");
Toast.makeText(context, "BROADCAST Screen ON", Toast.LENGTH_LONG).show();
}
}
}
该文档可能有一些错误,我已经开了两天头,并且我发现使用READ_PHONE_STATE权限,我的广播可以工作! (从4.3到5.1测试),请参阅下面的答案和代码,这里不适合。 – Josh
退房this tutorial关于广播接收机。
更新: 我不知道你正在使用本教程中,因为有很多有用的东西在本教程中,像这样:
@Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
换句话说,你已经创建一个自定义broadcast receiver class
,但你没有使用它。
也请检查this。
下添加了我的解决方案我正在使用本教程... –
我已更新我的答案.. –
您是否添加了所需的录像?
使用的许可机器人:名称=“android.permission.READ_PHONE_STATE”
这是一个接收器来处理屏幕关闭时或上或锁定:
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
}
}
} 这是清单:
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
为什么添加android.intent.action.USER_PRESENT?我不使用它 –
我想知道什么时候锁屏已经启动。然后我显示一张图片。因为我不想永远离开它。我需要知道用户何时在场。 –
这是否适合你? –
我有同样的问题,我固定它(在4.3和5.1测试)。我能够在清单中声明“android.intent.action.USER_PRESENT”,只要你有READ_PHONE_STATE权限,那就OK!我的迷你应用程序由一个广播接收器组成,可以响应屏幕开/关状态,并运行连续语音识别的后台服务。如果屏幕关闭,则关闭识别功能。下面是代码,即可享受:清单:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" >
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
广播接收器:
public class VoiceLaunchReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, VoiceLaunchService.class);
// service.putExtra(action, true);
Log.i("joscsr","Incoming Voice Launch Broadcast...");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.i("joshcsr", "************\nCSR Resumed (BC)\n************");
ctx.startService(service);
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************");
ctx.stopService(service);
}
}
}
你可以想像,我USER_PRESENT广播接收器没有注册任何其他地方。我在我的服务的onCreate方法中注册了ACTION_SCREEN_OFF和ON,这是我的接收方触发的。
@Override
public void onCreate() {
super.onCreate();
//Register screen ON/OFF BroadCast
launcher=new VoiceLaunchReceiver();
IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF);
i.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(launcher,i);
Log.d("joshcsr","VoiceLaunch Service CREATED");
}
最后我注销屏幕开/关中的onDestroy()我公司的服务:
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(launcher);}
http://stackoverflow.com/questions/7714731/broadcastreceiver-for-screen-lock-没有被触发你检查 –
谢谢它的作品。我在 –