Android活动本身就开始了吗?

时间:2013-07-19 10:03:27

标签: android broadcastreceiver alarmmanager

我有第一个活动 SimpleServiceController.java 如下:

public class SimpleServiceController extends Activity {

    BroadcastReceiver br;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button start = (Button) findViewById(R.id.serviceButton);
        Button stop = (Button) findViewById(R.id.cancelButton);

        start.setOnClickListener(startListener);
        stop.setOnClickListener(stopListener);
        Log.v("Steps", "1");
        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Log.v("Steps", "2");
                Toast.makeText(c, "Baboii!", Toast.LENGTH_LONG).show();
            }
        };
        Log.v("Steps", "3");
        registerReceiver(br, new IntentFilter("boom") );
        Log.v("Steps", "4");
    }
    private OnClickListener startListener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(SimpleServiceController.this,
                    SimpleUpdateService.class));
            startActivity(new Intent (SimpleServiceController.this, AlarmManagerListening.class));
        }
    };

我有另一项活动 AlarmManagerListening.java ,如下所示:

public class AlarmManagerListening extends Activity{
    final static private long ONE_SECOND = 1000;
    final static private long TEN_SECONDS = ONE_SECOND * 10;

    PendingIntent pi;
    BroadcastReceiver br;
    AlarmManager am;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pi = PendingIntent.getBroadcast( this, 0, new Intent("boom"), 0 );
        Log.v("Steps","5");
        am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
        Log.v("Steps","6");
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,          SystemClock.elapsedRealtime()+ONE_SECOND, TEN_SECONDS, pi);
        Log.v("Steps","7");
    }

逻辑上,只有在单击按钮时才应启动AlarmManagerListening活动。

然而,它本身已经开始了。有人可以向我解释代码吗?

非常感谢!

0 个答案:

没有答案