0

当我设置时间时,如何使用广播接收器在特定时间启动活动或执行一些操作,例如按钮单击操作(而不是吐司)?

4

2 回答 2

0

You might need AlarmManager + Service to do that. you can look it here

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

and in Service class (in the link) that display Toast, just make some new intent and startActivity(intent) there PS. before start. It's important that you must add this line also

youintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

like this

public class AlarmService extends Service {

private Intent alarmIntent;

@Override
public void onCreate() {
    alarmIntent = new Intent(this, AlarmActivity.class);
    alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    //Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
    //      .show();

    startActivity(alarmIntent);
}

// another implement method......
}
于 2012-08-15T04:47:27.893 回答
0

您可能需要处理程序;

Intent intent;
Handler handler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    intent = new Intent(this, ChildActivity.class);
    handler = new Handler();

    setContentView(R.layout.activity_streaming_client_example);


}

并使其在特定时间运行,使用 handler.postAtTime

handler.postAtTime(new Runnable() {
@Override
public void run() {
        MotherActivity.this.startActivityForResult(intent, 0);
    }
}, millisecond_toStartfromUpTime);

如果您想在几秒钟后发布,请使用 handler.postDelayed

handler.postDelayed(new Runnable() {
@Override
public void run() {
        MotherActivity.this.startActivityForResult(intent, 0);
    }
}, millisecond_delayhere);
于 2012-08-10T18:49:10.950 回答