任何Android專家?請幫忙解決這個...Android從服務類別到擴展服務類別的意圖
我有意向傳遞到數據服務類如下活動類
活動課
public class UsherActivity extends ListActivity {
...
public void createNotification() {
int subtotal = 580;
Intent serviceIntent = new Intent(this, SampleOverlayService.class);
serviceIntent.putExtra("box", "8");
serviceIntent.putExtra("tot", subtotal);
startService(serviceIntent);
}
}
我SampleOverlayService類如下:注意「subtotal_from_intent_putExtra_Here」 ...這是我想要顯示的數據。
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
public class SampleOverlayService extends OverlayService {
public static SampleOverlayService instance;
private SampleOverlayView overlayView;
@Override
public void onCreate() {
super.onCreate();
instance = this;
overlayView = new SampleOverlayView(this);
}
@Override
public void onDestroy() {
super.onDestroy();
if (overlayView != null) {
overlayView.destory();
}
}
static public void stop() {
if (instance != null) {
instance.stopSelf();
}
}
@Override
protected Notification foregroundNotification(int notificationId) {
Notification notification;
notification = new Notification(R.drawable.ic_launcher, "getString(R.string.title_notification), System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(this, "subtotal_from_intent_putExtra_Here", getString(R.string.message_notification), notificationIntent());
return notification;
}
private PendingIntent notificationIntent() {
Intent intent = new Intent(this, SampleOverlayHideActivity.class);
PendingIntent pending = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pending;
}
}
而且OverlayService類是如下
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class OverlayService extends Service {
protected boolean foreground = false;
protected boolean cancelNotification = false;
protected int id = 0;
protected Notification foregroundNotification(int notificationId) {
return null;
}
public void moveToForeground(int id, boolean cancelNotification) {
moveToForeground(id, foregroundNotification(id), cancelNotification);
}
public void moveToForeground(int id, Notification notification, boolean cancelNotification) {
if (! this.foreground && notification != null) {
this.foreground = true;
this.id = id;
this.cancelNotification = cancelNotification;
super.startForeground(id, notification);
} else if (this.id != id && id > 0 && notification != null) {
this.id = id;
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(id, notification);
}
}
public void moveToBackground(int id, boolean cancelNotification) {
foreground = false;
id = 0;
super.stopForeground(cancelNotification);
}
public void moveToBackground(int id) {
moveToBackground(id, cancelNotification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我誠實地嘗試不同的方法都無濟於事。我只是得到java.lang.nullPointerException,因爲intent.getExtras()返回null,無論我把它放在哪裏。如何獲得通知的小計?
在此先感謝。
編輯:我從https://gist.github.com/bjoernQ/6975256
你在哪裏打電話intent.getExtras()? – fluffyBatman
重寫函數onStartCommand,那你可以得到意圖 – has19
@has 19是的,我得到它被傳遞到OverlayService類的onStartCommand ...但我怎樣在SampleOverlayService類中調用它並將它分配給String? –