【问题标题】:NotificationManager not starting serviceNotificationManager 未启动服务
【发布时间】:2015-11-11 06:13:38
【问题描述】:

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

//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());
    }

这是加密服务

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。然而,这并没有发生。

【问题讨论】:

    标签: android service android-notifications android-pendingintent


    【解决方案1】:

    您缺少在acceptCryptoService 和dellCryptoService 意图中设置操作。

    acceptPendingIntent.setAction(StringResources.CRYPTO_ACTION);
    declineCryptoService.setAction(StringResources.CRYPTO_ACTION);
    

    【讨论】:

    • 操作不与明确的Intents 一起使用。此外,CRYPTO_ACTION 似乎是 Intent extra 的关键,而不是 Intent 操作。
    猜你喜欢
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2014-01-16
    • 2015-07-25
    • 2015-05-30
    • 2020-10-06
    相关资源
    最近更新 更多