单击通知后更改服务的值

Change a value of Service after clicking on a notification

大家好

我想做以下内容

一个服务在后台运行,当你 Sake phone 时显示一个通知,同时其他方法开始运行 "wait()",我想做的是,如果您单击通知,此服务的布尔参数会更改其值,以便根据您是否单击通知做出不同的想法。

我拥有的简化代码:

public class MyService extends Service implements SensorEventListener{

@Override
public void onSensorChanged(SensorEvent event) 
{

    if (acc(event.values[0], event.values[1], event.values[2]) >15)
    {

        notificacioAccident();

    }
}

private void sendSMS(String phoneNumber, String message) 
{

    synchronized (this) 
    {
        try {
            wait(waitTime);
        } catch (Exception e) {
        }
    }


    if(executar==true) 
    {
        //Do Something
    }
    else
    {
        //Do other think
    }
}

public void notificacioAccident() 
{
    Intent intent = new Intent(this,MyService.class);
    intent.putExtra("executar",false); //Dosen't work
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.crash_2_1);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app_icono));
    builder.setContentTitle(getResources().getString(R.string.justAccident));
    builder.setContentText(this.getResources().getText(R.string.notificacio_1) + telefon);
    builder.setSubText(this.getResources().getText(R.string.notificacio_2));
    builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);

    builder.setContentIntent(pendingIntent);//Prova
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

正如我所说,这不是所有代码,只关注当我单击创建 myService 的通知时如何将变量 "executar" 从 true 更改为 false。

谢谢 :D

终于找到了:

public class MyService extends Service implements SensorEventListener{

public int onStartCommand(Intent intent, int flags, int startId) {

    Bundle extras = intent.getExtras(); //Nou desde aqui
    if (extras != null) {
        executar=false;
    }                               //Fins aqui



@Override
public void onSensorChanged(SensorEvent event) 
{

if (acc(event.values[0], event.values[1], event.values[2]) >15)
{

    notificacioAccident();


    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {
        public void run() {
            sendSMS();
        }
    }, waitTime);

}
}

private void sendSMS() 
{


    if(executar==true) 
    {
        //Do Something
    }
    else
    {
         //Do other think
    }
}

public void notificacioAccident() 
{
    Intent intent = new Intent(this,MyService.class);
intent.putExtra("executar",false); 
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.crash_2_1);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app_icono));
    builder.setContentTitle(getResources().getString(R.string.justAccident));
    builder.setContentText(this.getResources().getText(R.string.notificacio_1) + telefon);
    builder.setSubText(this.getResources().getText(R.string.notificacio_2));
    builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);

    builder.setContentIntent(pendingIntent);//Prova
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}