作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 Intent 将额外内容发送到服务,然后该服务会打开一个应该接收 Intent 的额外内容的 Activity 。当我寻找它们时,额外内容在第二个 Activity 中似乎为空,
这里是代码 fragment 。
ToDoActivity.java fragment ( Activity )
public void sendNotification(String title, String body){
Calendar c= Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
c.set(mYear, mMonth, mDay, mhour, mminute, 0);
Intent intent = new Intent(this, MyAlarmService.class);
intent.putExtra(TO_DO_ITEM, body);
intent.putExtra(TO_DO_NAME, title);
intent.putExtra(TO_DO_TIME, c.getTimeInMillis());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent mAlarmSender = PendingIntent.getService(ToDoActivity.this, 0, intent, 0);
AlarmManager alm = (AlarmManager)getSystemService(ALARM_SERVICE);
alm.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), mAlarmSender);
Toast.makeText(this, "Alarm has been set for: " + body, Toast.LENGTH_LONG).show();
}
MyAlarmService.java(服务)
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Intent alert = new Intent();
try{
alert.putExtras(intent);
}catch(NullPointerException npe){
}
alert.setClass(this, Alert.class);
alert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(alert);
return START_REDELIVER_INTENT;
}
Alert.java fragment ( Activity )
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Builder alert = new AlertDialog.Builder(this);
Intent i = new Intent();
Bundle b = savedInstanceState;
String title="";
String itemToDo="";
long time =0;
try{
title = b.getString(ToDoActivity.TO_DO_NAME);
itemToDo = b.getString(ToDoActivity.TO_DO_ITEM);
time = b.getLong(ToDoActivity.TO_DO_TIME);
}catch (NullPointerException npe){
try{
title = i.getStringExtra(ToDoActivity.TO_DO_NAME);
itemToDo = i.getStringExtra(ToDoActivity.TO_DO_ITEM);
time = i.getLongExtra(ToDoActivity.TO_DO_TIME, System.currentTimeMillis());
}catch(NullPointerException npe2){
}
}
if((!title.equals("") && !itemToDo.equals("") && time !=0))
makeNotif(title, itemToDo, time);
alert.setTitle("Alert: Do the item on your to do list!!");
if(!itemToDo.equals(""))
alert.setMessage(itemToDo);
else
alert.setMessage("There is an item on your To Do List that needs to get done, please check the list and the time");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
mp.stop();
finish();
}
});
playSound(this, getAlarmUri());
alert.setIcon(R.drawable.ic_launcher);
AlertDialog ad = alert.create();
ad.show();
}
此方法在 Alert 中用于创建通知(另一个与 extras 有关的问题)信息也未通过此处显示(因为我将信息从以前的方法传递到此处)
static final int uniqueid= 139686;
public void makeNotif(String title, String body, long timeInMil){
try{
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, ToDoActivity.class);
final String itemToDo=intent.getStringExtra("TDL");
// final String title = b.getString("Name");
final String titleA = intent.getStringExtra("Name");
// final long time = b.getLong("Time");
final long time =intent.getLongExtra("Time",0);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
//Notification n = new Notification(0, body, System.currentTimeMillis());
//Notifaction n = Notification
Notification n = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(body)
//.setContentTitle(titleA)
//.setContentText(itemToDo)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(timeInMil)
.build();
//n.setLatestEventInfo(this, title, body, pi);
n.defaults=Notification.DEFAULT_ALL;
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(uniqueid, n);
}catch(Exception e){
e.printStackTrace();
}
}
如果有人可以告诉我为什么我的额外费用没有转移过来,那就太好了。
谢谢,Vnge
最佳答案
将您的 MyAlarmService onStartCommand 方法更改为将接收到的 Intent 值发送到警报 Activity :
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Intent alert = new Intent();
try{
alert.putExtra(TO_DO_ITEM, intent.getExtras().getString(TO_DO_ITEM));
alert.putExtra(TO_DO_NAME, intent.getExtras().getString(TO_DO_NAME));
alert.putExtra(TO_DO_TIME, intent.getExtras().getLong(TO_DO_TIME));
}catch(NullPointerException npe){
}
并使用 getIntent().getExtras()
在 Activity 中接收 Intent ,而不是在 Alert Activity 中使用 savedInstanceState
作为:
Bundle b = getIntent().getExtras();
if(b !=null){
title = b.getString(TO_DO_ITEM);
itemToDo = b.getString(TO_DO_NAME);
time = b.getLong(TO_DO_TIME);
}
关于Android:如何从 Activity 到 Service 到第二个 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14179951/
我是一名优秀的程序员,十分优秀!