我在监听程序服务中有一个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()
{
}
}我在我的MainActivity中启动了CryptoService。因此,后续的StartService调用应该调用onStartCommand。然而,这并没有发生。
发布于 2015-11-11 14:54:58
缺少在acceptCryptoService和declineCryptoService intent中设置操作。
acceptPendingIntent.setAction(StringResources.CRYPTO_ACTION);
declineCryptoService.setAction(StringResources.CRYPTO_ACTION);https://stackoverflow.com/questions/33645186
复制相似问题