首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NotificationManager未启动服务

NotificationManager未启动服务
EN

Stack Overflow用户
提问于 2015-11-11 14:13:38
回答 1查看 92关注 0票数 0

我在监听程序服务中有一个NotificationManager,我想在按下肯定按钮时启动一个服务。到目前为止,这并不起作用,我怀疑这可能与上下文有关。

代码语言:javascript
复制
//NotificationManager in GCM listener service

public class MyGcmListenerService extends GcmListenerService
{

    private static final String TAG = "MyGcmListenerService";
    private NotificationManager notificationManager;


    @Override
    public void onCreate()
    {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }

    @Override
    public void onMessageReceived(String from, Bundle data)
    {
        Log.i(TAG, "IP : " + (String) data.get("ip"));

        Intent acceptCryptoService = new Intent(this, CryptoService.class);
        acceptCryptoService.putExtra(StringResources.CRYPTO_ACTION, true);
        PendingIntent acceptPendingIntent = PendingIntent.getService(this, 0, acceptCryptoService, 0);

        Intent declineCryptoService = new Intent(this, CryptoService.class);
        declineCryptoService.putExtra(StringResources.CRYPTO_ACTION, false);
        PendingIntent declinePendingIntent = PendingIntent.getService(this, 0, declineCryptoService, 0);

        NotificationCompat.Action acceptAction = new NotificationCompat.Action
                .Builder(android.R.drawable.arrow_up_float, "Grant", acceptPendingIntent).build();

        NotificationCompat.Action declineAction = new NotificationCompat.Action
                .Builder(android.R.drawable.arrow_down_float, "Decline", declinePendingIntent).build();

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                .setContentTitle("New Password Request From " + (String) data.get("ip"))
                .addAction(acceptAction)
                .addAction(declineAction)
                .setSmallIcon(android.R.drawable.arrow_up_float)
                .setSmallIcon(android.R.drawable.arrow_down_float);

        notificationManager.notify(1, notification.build());
    }

这是CryptoService

代码语言:javascript
复制
public class CryptoService extends Service
{
    String TAG = "CryptoService";

    @Override
    public void onCreate()
    {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        boolean acceptCrypto = intent.getBooleanExtra(StringResources.CRYPTO_ACTION, false);
        Log.i(TAG, "accept crypt: " + acceptCrypto); //Not getting called
        return Service.START_NOT_STICKY;
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onDestroy()
    {

    }

}

我在我的MainActivity中启动了CryptoService。因此,后续的StartService调用应该调用onStartCommand。然而,这并没有发生。

EN

回答 1

Stack Overflow用户

发布于 2015-11-11 14:54:58

缺少在acceptCryptoService和declineCryptoService intent中设置操作。

代码语言:javascript
复制
acceptPendingIntent.setAction(StringResources.CRYPTO_ACTION);
declineCryptoService.setAction(StringResources.CRYPTO_ACTION);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33645186

复制
相关文章

相似问题

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