2015-01-02 20 views
0

我使用SharePreference設置組鬧鐘。在整個每件事情都很好,但我想用btn_cancel取消警報,但事實並非如此(儘管我使用StopService,但沒有任何事情發生!)。請幫我,我該怎麼辦?如何在Android中爲此Intent使用StopService?

public class MyActivity extends Activity implements View.OnClickListener { 

private static final String SHARED_PREFERENCES_NAME = "ALARM_NOTIFICATION"; 
public static final String REQUEST_CODE = "REQUEST_CODE"; 
public static final String TIME = "TIME"; 
public static final String ALARM_1_NAME = "ALARM_1"; 
public static final String ALARM_NAME = "ALARM_NAME"; 

public static TextView text1; 
private Button set1; 

public static long timer1 = 0; 
private int requestCode = 0; 
private Intent intent1 = null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    set1 = (Button) findViewById(R.id.btn_set1); 
    alarmLinear = (LinearLayout) findViewById(R.id.alarmLayout); 
    text1 = (TextView) findViewById(R.id.add1); 

    set1.setOnClickListener(this); 
    text1.setOnClickListener(this); 
    findViewById(R.id.btn_cancle1).setOnClickListener(this); 

} 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.add1: { 
      set1.setVisibility(View.VISIBLE); 

      if (timer1 == 0) { 
       timer1 = getCurrentTime(); 
      } // get time from system 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(timer1); //set time in timer parametr 

      TimePicker timePicker = (TimePicker) findViewById(R.id.tp_1); 
      timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); 
      timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE)); 



      break; 
     } 
     case R.id.btn_set1: { 
      TimePicker timePicker = (TimePicker) findViewById(R.id.tp_1); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); 
      calendar.set(Calendar.SECOND, 0); 
      timer1 = calendar.getTimeInMillis(); 

      String stringTime = convertTimeToString(timer1); 
      text1.setText(stringTime); 

      if (intent1 == null) { 
       intent1 = new Intent(this, alarmService.class); 
       intent1.putExtra(ALARM_NAME , ALARM_1_NAME); 
      } 
      requestCode++; 
      intent1.putExtra(REQUEST_CODE, requestCode); 
      intent1.putExtra(TIME, timer1); 
      startService(intent1); 
      break; 
     } 
     case R.id.btn_cancle1: 
      cancelTimers(); 
      break; 

    } 
} 

private long getCurrentTime() { 
    Calendar calendar = GregorianCalendar.getInstance(); 
    return calendar.getTimeInMillis(); 
} 


private void cancelTimers() { 
    if (intent1 != null) { 
     timer1 = 0; 
     stopService(intent1); 
     text1.setText("+"); 
    } 
} 

這AlarmService()類:

public class alarmService extends Service { 
@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

public int onStartCommand(Intent intent, int flags, int startId) { 
    if (intent == null) { 
     stopSelf(); 


    } else { 
     String alarmName = intent.getExtras().getString(MyActivity.ALARM_NAME); 
     Intent alarm = new Intent(this, alarm.class); 
     alarm.putExtra(MyActivity.ALARM_NAME,alarmName); 

     int requestCode = intent.getExtras().getInt(MyActivity.REQUEST_CODE); 
     long time = intent.getExtras().getLong(MyActivity.TIME); 

     PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, requestCode, alarm, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     long currentRTC = System.currentTimeMillis(); 
     if (currentRTC <= time) { 
      alarmManager.set(AlarmManager.RTC, time, pendingIntent1); 
     } 

    } 
    return START_STICKY; 
} 

}

感謝。

回答

1

這裏我們通過調用報警服務來取消報警管理器發出的報警。然後,我們將結果放入通過報警管理器訪問的結果中,並應用取消

Intent intent = new Intent(this, AlarmService.class); 
PendingIntent pendingIntent = 
    PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent); 
+0

這將是巨大的,如果你能解釋一下,你在這裏做什麼。 – blalasaadri

+1

這裏我們通過呼叫報警服務器來取消報警管理器的報警,並將報警服務器通過報警管理器置於待處理意圖中,並申請取消 –

+0

謝謝,我想用此代碼設置報警並對其進行管理(re - 設置和取消)由用戶。我的問題是取消BTN和如何取消這一過程。 –

1
 private void cancelTimers() { 
     if (intent1 != null) { 
      timer1 = 0; 
      intent1 = null; 
      stopService(intent1); 
      text1.setText("+"); 
     } 
    } 

改變這種代碼

+0

它不起作用。應用程序已停止 –

1

stopService犯規停止正在進行的報警必須停止這樣

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent); 
+0

謝謝,你的意思是我在MyActivity類的btn_cancel中使用此代碼? –

+0

是的,一旦與這 – KomalG

+0

檢查我介紹alarmManager在btn_set並取消它在btn_cancel。但它不是intent1 –

相關問題