2015-11-16 116 views
0

Hy全部點擊通知後更改服務值

我想進行以下操作。

服務在後臺運行,當您在電話中顯示通知時,同時其他方法開始以「wait()」運行時,我想要做的就是,如果您單擊通知這個服務的布爾參數改變他的價值,以使不同的想法,如果你點擊通知或不。

Symplifyed代碼,我有:

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

正如我已經說過了,這是不是所有的代碼,只專注於我如何可以改變從真到假變量「executar」當我點擊創建myService的通知。

謝謝:d

+0

爲什麼'intent.putExtra( 「executar」,假);''行這麼想的work'?你在onStartCommand方法中從Intent獲得價值嗎? –

+0

我不知道如何捕捉到這個值。 我不做任何關於onStartCommand()我必須做什麼? 考慮到我想改變的變量和notification()方法在同一個類中「我不知道它是否重要」 我在Android中很新,所以我是有點失落 –

+0

我覺得最終problema,我不知道我是如何解決這個問題: 的問題是,我要開始時,該代碼乳寧服務: 同步(這) { 試等待(waitTime); } catch(Exception e){ } } 而且這可能是不允許的,有任何方法可以做到這一點? 我再次解釋一下,當一個服務運行時,創建一個通知和一個等待,這取決於如果在等待時間點擊通知或不通知,不同的想法會發生 –

回答

0

終於讓我找到它:

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