首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android实现从服务类到扩展服务类的意图

Android实现从服务类到扩展服务类的意图
EN

Stack Overflow用户
提问于 2016-08-12 19:44:47
回答 2查看 450关注 0票数 2

有机器人专家吗?请帮我解决这个..。

我有一个活动类,它将意向数据传递给Service,如下所示

活动类

代码语言:javascript
复制
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"...this是我希望数据显示的地方。

代码语言:javascript
复制
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类如下所示

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-17 09:47:48

onStartCommand(Intent intent,, int flags, int startId)中重写SampleOverlayService

代码:

代码语言:javascript
复制
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);
    }

}

这将在totalCount变量中获得值,您可以相应地使用该值。

票数 0
EN

Stack Overflow用户

发布于 2016-08-12 20:23:55

意图被传递给'onStartCommand‘方法

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38925436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档