Hy all
我想做以下几点。
服务正在后台运行,当你给手机发出通知时,同时其他方法开始运行“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
答案 0 :(得分: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());
}