0

我是 android 新手,我的 Service 中有以下代码:

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

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

提前致谢。

4

2 回答 2

0

这就是我为服务所拥有的:(确保您在 onReceive 中使用正确的意图。onReceive 接收一个意图;启动服务的那个。我注意到当 Eclipse 创建 onReceive 时,Intent 参数的默认名称是“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"));
}
}

希望这可以帮助

于 2013-03-15T15:36:22.777 回答
0

如果您的活动正在运行,则新意图可能在 onNewIntent 中传递

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

    message = getIntent().getStringExtra("invoice");
    System.out.println(message); //always null 
} 
于 2013-03-15T18:21:16.987 回答