我实施了很多方法但没有获得所需的结果。 我想在Android中的单击按钮上调用2个方法。
我尝试下面的代码,但它对我不起作用。
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.savebtn:
startAlert();
default:
break;
}
}
public void startAlert() {
EditText Start_alarm = (EditText) findViewById(R.id.timeout_et);
int i = Integer.parseInt(Start_alarm.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ ((i * 1000)*60), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " minute",Toast.LENGTH_LONG).show();
// App Exit here
Intent intent_exit = new Intent(Intent.ACTION_MAIN);
intent_exit.addCategory(Intent.CATEGORY_HOME);
intent_exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent_exit);
finish();
SetUpdateInterval();
}
//-------Repeating Video---------
private void SetUpdateInterval() {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
EditText update_time = (EditText) findViewById(R.id.updateinterval_et);
int j = Integer.parseInt(update_time.getText().toString());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * j, pendingIntent);
}
现在,使用此代码只调用一个方法,即StartAlert()。我也想调用SetUpdateInterval方法。 那么任何人都可以告诉我,我如何同时运行这两种方法。
提前谢谢。
简化代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.savebtn:
startAlert();
SetUpdateInterval();
finish();
break;
case R.id.cancelbtn:
finish();
break;
default:
break;
}
}
public void startAlert() {
int i = Integer.parseInt(timeout_et.getText().toString());
System.out.println("VAlue of i" +" "+i);
int j = Integer.parseInt(update_et.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ ((i * 1000)*60), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ ((j * 1000)*60), j, pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " minute",Toast.LENGTH_LONG).show();
}
//-------Repeating Video---------
private void SetUpdateInterval() {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
EditText update_time = (EditText) findViewById(R.id.updateinterval_et);
int j = Integer.parseInt(update_time.getText().toString());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * j, pendingIntent);
// App Exit here
Intent intent_exit = new Intent(Intent.ACTION_MAIN);
intent_exit.addCategory(Intent.CATEGORY_HOME);
intent_exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent_exit);
finish();
}
答案 0 :(得分:0)
在开始其他活动并完成当前活动之前调用SetUpdateInterval()
方法。
答案 1 :(得分:0)
只需替换下面的代码,您就可以在调用该方法之前完成活动:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.savebtn:
startAlert();
SetUpdateInterval()
default:
break;
}
}
//将下面的代码放在SetUpdateInterval()方法中一段延迟,然后完成。
new Timer().schedule(new TimerTask() {
@Override
public void run() {
finish();
}
},3000);