从服务发送到活动的数据已丢失。为什么?

时间:2013-03-15 14:37:30

标签: 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) 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);

我可以看到我传递给意图的message存在(即它不是null)。但是当我尝试获取InvoiceActivity值时,在我的message方面,我得到null所有时间。我在InvoiceActivity中使用以下代码:

 Bundle extras = getIntent().getExtras();
 message = extras.getString("invoice");
 System.out.println(message); //always null

我不确定为什么会这样。我在哪里弄错了?

提前致谢。

2 个答案:

答案 0 :(得分:0)

这就是我对服务的要求:(确保在onReceive中使用正确的意图.onReceive接收一个意图;启动服务的意图。我注意到当Eclipse创建onReceive时,Intent参数的默认名称是“意图”(你不想使用这个)。另外,为什么你使用变量上下文而不是关键字this?Service类扩展ContextWrapper,它扩展了Context。

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.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        startService(new Intent(this,Serv.class));
    }
    @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;
    }
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    stopService(new Intent(this,Serv.class));
}
}

这里是InvoiceActivity

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"));
}
}

希望这有帮助

答案 1 :(得分:0)

如果您的活动正在运行,新意图可能会在onNewIntent

中传递
@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    message = getIntent().getStringExtra("invoice");
    System.out.println(message); //always null 
}