-1

此服务中的代码我尝试将数据从服务发送到活动,但它不起作用,

public void onStart(Intent intent, int startId) {

// TODO Auto-generated method stub

  super.onStart(intent, startId);
  Toast.makeText(this, "onstart()",Toast.LENGTH_SHORT).show();

  String message = intent.getStringExtra("msg");
  int ID = intent.getExtras().getInt("id");
  Toast.makeText(getApplication(), message, Toast.LENGTH_LONG).show();
  NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

  Intent notificationintent = new Intent(this, Details.class);

  notificationintent.putExtra("ID", ID);

  PendingIntent pendingintent = PendingIntent.getActivity(this,ID, notificationintent,0);

  int icon = R.drawable.ic_launcher;
  String tickerText = "you have a massege";
  long when = System.currentTimeMillis();
  Notification notification = new Notification(icon, tickerText, when);

  String contentTitle = "Title";
  String contentText = message;
  notification.setLatestEventInfo(this, contentTitle, contentText, pendingintent);
  notification.defaults = notification.DEFAULT_SOUND;

  notificationmanager.notify(ID, notification);

这是活动

Intent i = new Intent();
int num = i.getExtras().getInt("ID");
number.setText(num); // number is textview 
4

2 回答 2

2

像这样更改您的活动代码 -

  int num = getIntent().getIntExtra("ID", 0);

  number.setText(String.valueOf(num)); // number is textview 
于 2013-09-14T20:09:02.520 回答
0

我不认为您可以直接从服务通信到活动。请记住,服务可能独立于任何活动运行。您可以通过 startService 调用从活动与服务进行通信,传递一个意图。

为了与活动通信,活动必须绑定到服务:http: //developer.android.com/guide/components/bound-services.html

另一个选项是服务可以发送广播,活动可以通过传递给广播接收器的处理程序获取消息。

于 2013-09-14T20:12:10.723 回答