码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- 闹钟服务的使用(单次闹钟)

时间:2014-12-14 14:32:29      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

1. 效果图

bubuko.com,布布扣

2. 实现代码

bubuko.com,布布扣
public class firstActivity extends Activity {
    
    private static final String AM_ACTION = "iflab.test.AM_ACTION"; // ACTION常量

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        Button btn1 = (Button) findViewById(R.id.button1); // 获取对象

        final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);// 获取系统服务
        Intent intent = new Intent(); // Intent对象
        intent.setAction(AM_ACTION); // 设置Action
        intent.putExtra("message", "该下班啦!"); // 传递字符串
        final PendingIntent pendingIntent = PendingIntent.getBroadcast(
                firstActivity.this, 0, intent, 0);
        final long time = System.currentTimeMillis(); // 设置当前时间

        btn1.setOnClickListener(new View.OnClickListener() { // 监听器

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); // 设置单次闹钟提醒
            }
        });
    }
}
firstActivity
bubuko.com,布布扣
public class myReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        String str;
        str="触发单次闹钟:"+arg1.getStringExtra("message");
        Toast.makeText(arg0, str,Toast.LENGTH_LONG).show();        //显示
    }
}
myReceiver
bubuko.com,布布扣
  <receiver android:name="myReceiver">
            <intent-filter>
                <action android:name="iflab.test.AM_ACTION"/>
            </intent-filter>
        </receiver>
配置文件

 

Android -- 闹钟服务的使用(单次闹钟)

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/SM-t/p/4162526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!