Tom*_*ume 9 android bundle broadcastreceiver alarm android-intent
我有一个Android应用程序需要在一天中偶尔被唤醒.
为此,我使用AlarmManager设置PendingIntent并使此触发器成为BroadcastReceiver.然后,此BroadcastReceiver启动一个Activity以将UI带到前台.
所有上述内容似乎都有效,因为Activity正确启动; 但是我希望BroadcastReceiver通知Activity它是由警报启动的(而不是由用户启动).要做到这一点,我正在尝试从BroadcastReceiver的onReceive()方法在intent的extras包中设置一个变量,因此:
Intent i = new Intent(context, MyActivity.class);
i.putExtra(wakeupKey, true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
在我的Activity的onResume()方法中,然后我寻找这个布尔变量的存在:
protected void onResume() {
super.onResume();
String wakeupKey = "blah";
if (getIntent()!=null && getIntent().getExtras()!=null)
Log.d("app", "onResume at " + System.currentTimeMillis() + ":" + getIntent().getExtras().getBoolean(wakeupKey));
else
Log.d("app", "onResume at " + System.currentTimeMillis() + ": null");
}
Run Code Online (Sandbox Code Playgroud)
onResume()中的getIntent().getExtras()调用总是返回null - 我似乎无法在此bundle中传递任何额外的东西.
如果我使用相同的方法将额外的东西绑定到触发BroadcastReceiver的PendingIntent,那么额外的东西就可以了.
任何人都可以告诉我将一个bundle从BroadcastReceiver传递给Activity有什么不同,而不是将bundle从一个Activity传递给一个BroadcastReceiver?我担心我可能会做一些非常明显错误的事情......
Nor*_*ldt 30
只是为了说清楚(因为我花了很多时间弄清楚如何使它工作)
在扩展的服务类中BroadcastReceiver.输入以下代码onReceive()
Intent intent2open = new Intent(context, YourActivity.class);
intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String name = "KEY";
String value = "String you want to pass";
intent2open.putExtra(name, value);
context.startActivity(intent2open);
Run Code Online (Sandbox Code Playgroud)
在FLAG_ACTIVITY_SINGLE_TOP可以确保应用不会重新打开,如果已经打开.这意味着首先打开YourActivity的"旧"意图将被重用,并且不会包含额外的值.你必须在另一个调用onNewIntent()的方法中捕获它们YourActivity.
public class YourActivity extends Activity {
private String memberFieldString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Code doing your thing...
} // End of onCreate()
@Override
protected void onNewIntent(Intent intent) {
Log.d("YourActivity", "onNewIntent is called!");
memberFieldString = intent.getStringExtra("KEY");
super.onNewIntent(intent);
} // End of onNewIntent(Intent intent)
@Override
protected void onResume() {
if (memberFieldString != null) {
if (opstartsIntent.getStringExtra(KEY) != null) {
Log.d("YourActivity", "memberFieldString: "+ memberFieldString);
} else {
Log.d("YourActivity", "The intent that started YourActivity did not have an extra string value");
}
}
} // End of onResume()
} // End of YourActivity
Run Code Online (Sandbox Code Playgroud)
请注意两个if语句 - onResume()不知道它是否被调用OnCreate()->OnStart() OR onRestart()->onStart()
请参阅: http ://www.anddev.org/images/android/activity_lifecycle.png
它仅用于测试应用程序是由用户启动(意图没有额外的)还是由BroadcastReceiver(意图与额外).
然后,此BroadcastReceiver启动一个Activity以将UI带到前台.
这可能是你问题的症结所在.尝试覆盖onNewIntent()并查看Intent传递给它的是否有额外的.如果是这样,那是因为您在清单中设置活动的方式(例如,您正在使用singleTop)以及在此特定情况下活动已经存在的事实.
您也可以考虑摆脱,i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);看看这些变化是否重要.
结果是你正在做的事情 - 把额外的东西放在Intentfor startActivity()- 应该工作得很好.事实上,你可以在这里看到这方面的例子.这表明它对于活动(例如singleTop)或您调用活动的方式(例如)来说是一种时髦的东西FLAG_ACTIVITY_NEW_TASK.
编辑:
由于我的第一次拍摄不起作用,并给出了上面的"curiouser"评论......
这感觉有点像PendingIntent- 有了这些,除非你采取措施,否则你将无法更新额外内容.
一时兴起,尝试<intent-filter>在清单中为活动添加一秒,只需在一些独特的操作字符串上添加,然后尝试使用它从接收者开始您的活动.或者,只需将一些动作字符串扔进Intent接收器正在使用的行为中startActivity(),而不会弄乱清单.
| 归档时间: |
|
| 查看次数: |
22676 次 |
| 最近记录: |