0
我在侦听器服务中有NotificationManager
,我想在按下肯定按钮时启动服务。到目前为止,这不起作用,我怀疑它可能与上下文有关。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
。但是,这不是发生。
操作不用于明确的'意图'。另外,看起来'CRYPTO_ACTION'是'Intent'额外的关键,而不是'Intent'动作。 –