我在侦听器服务中有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());
}
这是CryptoService
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()
{
}
}
我在CryptoService
开始了MainActivity
。因此,后续StartService
次调用应调用onStartCommand
。但是,这不会发生。
答案 0 :(得分:0)
您缺少在acceptCryptoService和declineCryptoService意图中设置操作。
acceptPendingIntent.setAction(StringResources.CRYPTO_ACTION);
declineCryptoService.setAction(StringResources.CRYPTO_ACTION);