Android 从服务发送到活动的数据已丢失。为什么?
我是android新手,我的服务中有以下代码:Android 从服务发送到活动的数据已丢失。为什么?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我是android新手,我的服务中有以下代码: int icon = R.drawable.gcm_notification; long when = System.currentTimeMillis(); String notificationText = "Message From SimplePay"; //get the notification service NotificationManager notificationManager = (NotificationManager
int icon = R.drawable.gcm_notification;
long when = System.currentTimeMillis();
String notificationText = "Message From SimplePay";
//get the notification service
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon,notificationText,when);
String title = context.getString(R.string.app_name);
String messageBody = "You have " + 1 + " payment request pending";
Intent notificationIntent = new Intent(context,InvoiceActivity.class);
notificationIntent.putExtra("invoice",message);
System.out.println(message);
//always create a new activity! So recent values get updated
PendingIntent intent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context,title,messageBody,intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,notification);
我可以看到我传递给intent的消息
是存在的(即它不是null
)。但是在我的InvoiceActivity
方面,当我试图获取消息
值时,我总是得到null
。我在InvoiceActivity
中使用以下代码:
Bundle extras = getIntent().getExtras();
message = extras.getString("invoice");
System.out.println(message); //always null
我不知道为什么会这样。我在哪里犯了错误
提前感谢。这是我为该服务准备的:(确保您在onReceive中使用了正确的意图。onReceive接收到一个意图;启动该服务的意图。我注意到,当Eclipse创建onReceive时,意图参数的默认名称是“intent”(您不想使用此名称)。另外,为什么要使用变量上下文而不是关键字this?服务类扩展了扩展上下文的ContextWrapper
package com.example.solution;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class Serv extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
String message = "Hello";
long when = System.currentTimeMillis();
String notificationText = "Message From SimplePay";
//get the notification service
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notificationText,when);
String title = this.getString(R.string.app_name);
String messageBody = "You have " + 1 + " payment request pending";
Intent notificationIntent = new Intent(this,InvoiceActivity.class);
notificationIntent.putExtra("invoice",message);
//always create a new activity! So recent values get updated
PendingIntent intentp = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(this,title,messageBody,intentp);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,notification);
return START_STICKY;
}
}
以下是主要活动(启动器)
这是发票活动
package com.example.solution;
import android.app.Activity;
import android.util.Log;
public class InvoiceActivity extends Activity {
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("MESSAGE",getIntent().getExtras().getString("invoice"));
}
}
希望这对您有所帮助如果您的活动正在运行,那么新的意图可能会在onNewIntent中实现
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
message = getIntent().getStringExtra("invoice");
System.out.println(message); //always null
}
你用哪种方法抓住了这个机会data@StinePike:OnCreate方法。
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
message = getIntent().getStringExtra("invoice");
System.out.println(message); //always null
}