Android组件通讯之BroadcastReceiver应用闹钟服务

此应用简单的使用了BroadcastReceiver接收一个闹钟时间传送的PendingIntent,然后通过BroadcastReceiver启动一个Activity来提示闹钟到时信息

首先写闹钟到时信息提示的Activity:

View Code
 1 public class AlarmMsgActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         // 设置闹钟时间到达时跳转到此Activity弹出的Dialog
 7         new AlertDialog.Builder(AlarmMsgActivity.this)
 8         .setTitle("闹钟已经响起")
 9         .setMessage("现在时间为:" +  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
10         .setPositiveButton("取消", new DialogInterface.OnClickListener() {
11             
12             @Override
13             public void onClick(DialogInterface dialog, int which) {
14                 // 结束此Activity
15                 AlarmMsgActivity.this.finish();
16             }
17         })
18         .show();
19     }
20     
21 }

 

然后编写BroadcastReceiver:

View Code
 1 public class AlarmReceiver extends BroadcastReceiver {
 2 
 3     @Override
 4     public void onReceive(Context context, Intent intent) {
 5         // 此广播接收者一旦接收到广播,就会跳转到AlarmMsgActivity
 6         Intent alarmIntent = new Intent(context, AlarmMsgActivity.class);
 7         // 设置跳转的Activity为一个新Task,此标记详解见http://www.cnblogs.com/xiaoQLu/archive/2012/07/17/2595294.html
 8         alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 9         context.startActivity(alarmIntent);
10     }
11 
12 }

 

别忘了注册两个新组件:

<!-- 注册广播接收器 -->
        <receiver android:name=".AlarmReceiver">
            <intent-filter >
                <action android:name="com.moka.ALARM"/>                
            </intent-filter>
        </receiver>
        <!-- 注册目的Activity -->
        <activity android:name=".AlarmMsgActivity"></activity>

 

最后写设置闹钟的Activity:

View Code
public class MainActivity extends Activity {
    
    private TimePicker timePicker = null;
    private TextView alarmInfoText = null;
    private Button setAlarmBtn = null;
    private Button deleteAlarmBtn = null;
    
    private AlarmManager manager = null;
    // Calendar用来实现普通时间与长整形时间的格式转换
    private Calendar calendar = Calendar.getInstance();
    // 用来填充TextView
    private int hourOfday = 0;
    private int minute = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        alarmInfoText = (TextView) findViewById(R.id.alarmInfoText);
        setAlarmBtn = (Button) findViewById(R.id.setAlarmBtn);
        deleteAlarmBtn = (Button) findViewById(R.id.deleteAlarmBtn);
        
        timePicker.setOnTimeChangedListener(new TimeChangedListener());
        setAlarmBtn.setOnClickListener(new SetAlarmBtnListener());
        deleteAlarmBtn.setOnClickListener(new DeleteAlarmBtnListener());
        
        // 获取系统闹钟服务
        manager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    }
    
    private class TimeChangedListener implements OnTimeChangedListener {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TimePicker改变时,设置calendar对应值
            MainActivity.this.calendar.setTimeInMillis(System.currentTimeMillis()) ;
            MainActivity.this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            MainActivity.this.calendar.set(Calendar.MINUTE, minute);
            // 注意,若此项不设置,就不会在设定的分钟数刚到就激活闹钟,而是在未来1分钟内随机触发
            MainActivity.this.calendar.set(Calendar.SECOND, 0);
            
            MainActivity.this.hourOfday = hourOfDay;
            MainActivity.this.minute = minute;
            
        }
        
    }
    
    private class SetAlarmBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // 准备封装intent
            Intent intent = new Intent();
            intent.setAction("com.moka.ALARM");
            PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            // 第一参数:类型, 第二参数:设定的闹钟时间(长整型), 第三参数:满足时间到达条件后,处理接下来事件的PendingIntent
            manager.set(AlarmManager.RTC_WAKEUP, MainActivity.this.calendar.getTimeInMillis(), pi);
            
            MainActivity.this.alarmInfoText.setText("设定闹钟时间为:" + MainActivity.this.hourOfday + ":" + MainActivity.this.minute);
        }
        
    }
    
    private class DeleteAlarmBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (MainActivity.this.manager != null) {
                // 准备封装intent
                Intent intent = new Intent();
                intent.setAction("com.moka.ALARM");
                PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                // 取消设定闹钟
                manager.cancel(pi);
                MainActivity.this.alarmInfoText.setText("请设定闹钟");
            }
        }
        
    }
    
}

 

效果如图:

 

posted @ 2013-05-08 12:10  来杯冰镇魔卡  阅读(459)  评论(0编辑  收藏  举报