[英]Android Getting Intent from Service Class to extended Service Class
Any Android gurus around? 周围有Android专家吗? Please help solve this...
请帮助解决此问题...
I have an activity class that passes Intent data to Service Class as follows 我有一个将Intent数据传递给Service Class的活动类,如下所示
Activity Class 活动课
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);
}
}
My SampleOverlayService Class is as follows: Note the "subtotal_from_intent_putExtra_Here"...this is where i would like the data to show. 我的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;
}
}
And the OverlayService Class is as follows 并且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;
}
}
I've honestly tried different approaches to no avail. 老实说,我尝试了不同的方法都没有用。 I just get java.lang.nullPointerException since the intent.getExtras()... returns null wherever I place it.
我只得到java.lang.nullPointerException,因为intent.getExtras()...不论放置在哪里都返回null。 How do I get subtotal into the notification?
如何汇总到通知中?
Thanks in advance. 提前致谢。
Edit: I'm basing from https://gist.github.com/bjoernQ/6975256 编辑:我基于https://gist.github.com/bjoernQ/6975256
意图传递给“ onStartCommand”方法
Override onStartCommand(Intent intent,, int flags, int startId)
in SampleOverlayService
. 覆盖
onStartCommand(Intent intent,, int flags, int startId)
在SampleOverlayService
。
Code : 代码:
public class SampleOverlayService extends OverlayService {
private int totalCount = 0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
totalCount = intent.getIntExtra("tot", 0);
return super.onStartCommand(intent, flags, startId);
}
}
This will get value in totalCount
variable and you can use this value accordingly. 这将在
totalCount
变量中获取值,您可以相应地使用此值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.