1
我的應用程序正在任何特定時間發出通知。我正在使用notificationManager所在的BroadcastReceiver和Service類。 我需要將一些數據(項目標題)傳遞給通知。 什麼是最好的方法?Android:如何將數據從活動傳遞到服務?
這是主要活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day);
Intent myIntent = new Intent(Config.context, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Config.context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)Config.context.getSystemService(Config.context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, 24 * 60 * 60 * 1000, pendingIntent);
}
這裏是我的BrodacastReceiver
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String title = intent.getStringExtra(DayActivity.EXTRA_TITLE);
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
在這裏,服務類
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),DayActivity.class);
Notification notification = new Notification(R.drawable.notification_logo, "Title", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "Title", "Message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
我試圖用意圖額外的,但它不工作時該應用程序已關閉。
http://stackoverflow.com/questions/15234181/發送數據從活動到服務 – Pavlos 2014-09-28 18:31:43