我正在尝试学习使用Android BroadcasReceiver。 我写了这段代码,但它不起作用......我试着举例来改变时间...... 这有什么不对?
我在Manifest中添加了:
<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;
}
}
答案 0 :(得分:2)
我解决了!
“与其他广泛的意图不同,对于Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON,你不能在你的Android Manifest中声明它们!我不确定为什么,但它们必须在你的JAVA代码中的IntentFilter中注册”
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扩展了Activity {
@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);
}
}
我的广播接收器
public class Broadcast扩展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();
}
}
}
答案 1 :(得分:0)
查看有关广播接收器的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。
答案 2 :(得分:0)
您是否添加了所需的预设?
uses-permission android:name =“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>
答案 3 :(得分:0)
我遇到了同样的问题,我修复了它(在4.3和5.1上测试过)。我可以在清单中声明“android.intent.action.USER_PRESENT”,只要你有READ_PHONE_STATE权限,就可以了!我的迷你应用程序包括一个广播接收器,它响应屏幕的开/关状态,并运行一个连续语音识别的后台服务。如果屏幕关闭,则关闭识别。这是代码,享受:MANIFEST:
<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);}