我試圖讓對話框觸發一個意圖,然後關閉。使用意圖啓動服務後關閉對話框
public class PinMessage extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Random random = new Random();
final int pin = random.nextInt(9000) +1000;
builder.setMessage("Your pin is " + (pin)+". Please note in case of password loss.")
.setTitle("Security PIN")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences launch_track = PreferenceManager
.getDefaultSharedPreferences(MainActivity.context);
SharedPreferences.Editor editor1 = launch_track.edit();
editor1.putInt("pin", pin);
editor1.commit();
//This triggers before the dialog closes
Intent lock = new Intent(context,AppLockService.class);
context.startService(lock);
// FIRE ZE MISSILES!
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
單擊確定按鈕,除了觸發意圖和啓動服務之外,我還希望關閉對話框。
當前,當我按OK時,意圖被觸發並且服務在後臺啓動,但對話框沒有關閉,我必須按下電話按鈕才能退出。請幫幫我。
哈哈。它非常簡單。感謝您的解決方案。 :) – SoulRayder
+1與您的答案:) –
接受答案,如果它爲你工作:) –