|
9幾個相關(guān)類如下(也可從附件中直接下載):
希望能實現(xiàn)的效果是在一定時間后,比如半小時,甚至幾天后,可以自動啟動MyActivity,
package hyc.android.ui;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class AutoSyncWindowActivity extends Activity implements OnCheckedChangeListener{
private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT;
private RadioGroup rg_layout;
private RadioGroup.LayoutParams params;
private final static int CANCEL_AUTOSYNC = 1001;
private final static int AUTOSYNC_EVERY_WEEK = 1002;
private final static int AUTOSYNC_EVERY_MONTH = 1003;
private static AutoSyncWindowActivity appRef = null;
protected boolean isChange;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appRef = this;
rg_layout = new RadioGroup(this);
rg_layout.setOrientation(RadioGroup.VERTICAL);
params = new RadioGroup.LayoutParams(WC,WC);
rg_layout.setLayoutParams(params);
rg_layout.setOnCheckedChangeListener(this);
RadioButton cancel = new RadioButton(this);
cancel.setText("取消自動同步");
cancel.setId(CANCEL_AUTOSYNC);
params = new RadioGroup.LayoutParams(WC,WC);
rg_layout.addView(cancel, params);
RadioButton everyWeek = new RadioButton(this);
everyWeek.setText("每周自動同步");
everyWeek.setId(AUTOSYNC_EVERY_WEEK);
params = new RadioGroup.LayoutParams(WC,WC);
rg_layout.addView(everyWeek, params);
RadioButton everyMonth = new RadioButton(this);
everyMonth.setText("每月自動同步");
everyMonth.setId(AUTOSYNC_EVERY_MONTH);
params = new RadioGroup.LayoutParams(WC,WC);
rg_layout.addView(everyMonth, params);
setContentView(rg_layout);
// setContentView(R.layout.main);
}
public static AutoSyncWindowActivity getApp(){
return appRef;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(isChange)
return;
if(group.getCheckedRadioButtonId() == CANCEL_AUTOSYNC){
newEvent("Here is a first title!");
// autoSyncEveryWeek();
}else if(group.getCheckedRadioButtonId() == AUTOSYNC_EVERY_WEEK){
// newEvent("Here is first title!");
autoSyncEveryWeek();
}else if(group.getCheckedRadioButtonId() == AUTOSYNC_EVERY_MONTH){
noEvent();
}
//改變被選中按鈕的文字大小
RadioButton radio = (RadioButton)findViewById(checkedId);
if(radio != null)
radio.setTextSize(25);
//加入了以下這句代碼,時間一到AlarmManager機制仍有起到作用,只是報錯,無法啟動
//所需要的的MyActivity
//AutoSyncWindowActivity.this.waiTCLosing();
// TODO Auto-generated method stub
}
public void newEvent(String data){
setTitle(data);
}
public void noEvent(){
}
private void autoSyncEveryWeek(){
Intent intent = new Intent(AutoSyncWindowActivity.this, AlarmBroadcastReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 15);
AlarmManager alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
//關(guān)閉程序
protected void waitClosing() {
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
AutoSyncWindowActivity.this.finish();
}
}.start();
}
}
|
上一篇: android訪問Tomcat,實現(xiàn)數(shù)據(jù)的提交下一篇: 遍歷3gp視頻文件
|