0

我有第一个活动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只有在单击按钮时才应该启动活动。

但是,它是自己启动的。谁能给我解释一下代码?

非常感谢!

4

0 回答 0