标签:android 系统服务 闹钟服务 service alarmmanager
Android提供的系统服务之--AlarmManager(闹钟服务)
——转载请注明出处:coder-pig
本节引言:
本节主要介绍的是Android系统服务中的---AlarmManager(闹钟服务),
除了开发手机闹钟外,更多的时候是作为一个全局的定时器,通常与Service
结合,在特定时间启动其他的组件!本节就来对这个AlarmManager来进行解析
同时通过小闹钟与自动换壁纸来演示这个AlarmManager的用法,好了,开始本节的
内容吧!
本节正文:
好了,光看不练可不行,下面就来写两个简单的例子:
分别是定时闹钟提醒的实现与定时更换手机壁纸,一个是调用Activity,一个是调用Service
MainActivity.java:
package com.jay.example.alarmmanagerdemo; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSetClock; private Button btnbtnCloseClock; private AlarmManager alarmManager; private PendingIntent pi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSetClock = (Button) findViewById(R.id.btnSetClock); btnbtnCloseClock = (Button) findViewById(R.id.btnCloseClock); // ①获取AlarmManager对象: alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); // 指定要启动的是Activity组件,通过PendingIntent调用getActivity来设置 Intent intent = new Intent(MainActivity.this, ClockActivity.class); pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); btnSetClock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Calendar currentTime = Calendar.getInstance(); // 弹出一个时间设置的对话框,供用户选择时间 new TimePickerDialog(MainActivity.this, 0, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //设置当前时间 Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); // 根据用户选择的时间来设置Calendar对象 c.set(Calendar.HOUR, hourOfDay); c.set(Calendar.MINUTE, minute); // ②设置AlarmManager在Calendar对应的时间启动Activity alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); // 提示闹钟设置完毕: Toast.makeText(MainActivity.this, "闹钟设置完毕~", Toast.LENGTH_SHORT).show(); } }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime .get(Calendar.MINUTE), false).show(); btnbtnCloseClock.setVisibility(View.VISIBLE); } }); btnbtnCloseClock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alarmManager.cancel(pi); btnbtnCloseClock.setVisibility(View.GONE); Toast.makeText(MainActivity.this, "闹钟已取消", Toast.LENGTH_SHORT) .show(); } }); } }
ClockActivity.java:
package com.jay.example.alarmmanagerdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.media.MediaPlayer; import android.os.Bundle; public class ClockActivity extends Activity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clock); mediaPlayer = MediaPlayer.create(this, R.raw.pig); //mediaPlayer.setLooping(true); mediaPlayer.start(); //创建一个闹钟提醒的对话框,点击确定关闭铃声与页面 new AlertDialog.Builder(ClockActivity.this).setTitle("闹钟").setMessage("小猪小猪快起床~") .setPositiveButton("关闭闹铃", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mediaPlayer.stop(); ClockActivity.this.finish(); } }).show(); } }
运行截图:
总结核心流程吧:
①AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
获得系统提供的AlarmManager服务的对象
②Intent设置要启动的组件:
Intent intent = new Intent(MainActivity.this, ClockActivity.class);
③PendingIntent对象设置动作,启动的是Activity还是Service,又或者是广播!
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
④调用AlarmManager的set( )方法设置单次闹钟的闹钟类型,启动时间以及PendingIntent对象!
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pi);
从上面的代码我们发现AlarmManager的用法其实就那么几步,很简单,接下来我们弄一个Service
这里我们涉及到另外一个API: WallpaperManager,管理壁纸的,提供了下述几个方法供我们使用:
setBitmap(Bitmap bitmap),setResource(int resid),setStream(InputStream data);
还有一个清除壁纸的clean方法!
流程和前面的闹钟原理差不多,就不讲解了,直接贴代码吧:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.alarmmanagerdemo2.MainActivity" > <Button android:id="@+id/btnOn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启自动换壁纸" /> <Button android:id="@+id/btnOff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭自动换壁纸" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除壁纸" /> </LinearLayout>
package com.example.alarmmanagerdemo2; import java.io.IOException; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.WallpaperManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnOn; private Button btnOff; private Button btnCancel; private AlarmManager alarmManager; private PendingIntent pi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //①获得AlarmManager对象: alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);; //②指定要启动的Service,并指明动作是Servce: Intent intent = new Intent(MainActivity.this,WallPagerService.class); pi = PendingIntent.getService(MainActivity.this, 0, intent, 0); btnOn = (Button) findViewById(R.id.btnOn); btnOff = (Button) findViewById(R.id.btnOff); btnCancel = (Button) findViewById(R.id.btnCancel); btnOn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi); btnOn.setEnabled(false); btnOff.setEnabled(true); Toast.makeText(MainActivity.this, "自动更换壁纸设置成功", Toast.LENGTH_SHORT).show(); } }); btnOff.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btnOn.setEnabled(true); btnOff.setEnabled(false); alarmManager.cancel(pi); } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { WallpaperManager.getInstance(getApplicationContext()).clear(); Toast.makeText(MainActivity.this, "清除壁纸成功~", Toast.LENGTH_SHORT).show(); } catch (IOException e) {e.printStackTrace();} } }); } }
package com.example.alarmmanagerdemo2; import android.app.Service; import android.app.WallpaperManager; import android.content.Intent; import android.os.IBinder; public class WallPagerService extends Service { //定义当前壁纸下表 int current = 0; //获取壁纸资源id数组 private int[] paper = new int[] { R.drawable.gui_1, R.drawable.gui_2, R.drawable.gui_3, R.drawable.gui_4, }; //定义WallpaperManager服务 private WallpaperManager wManager; //初始化WallPagerManager @Override public void onCreate() { super.onCreate(); wManager = WallpaperManager.getInstance(this); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if(current >= 4)current = 0; try{ wManager.setResource(paper[current++]); }catch(Exception e){e.printStackTrace();} return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } }
<uses-permission android:name="android.permission.SET_WALLPAPER" />
运行效果图:
最后说两句:
就是这么简单,关于这个AlarmManager(闹钟服务)就学习到这里吧,在后面我们在Service实例讲解
处还会做一个后台定时发送短信的例子,敬请期待~
1.定时闹钟:http://pan.baidu.com/s/1qW9eiWc
2.定时切换壁纸:http://pan.baidu.com/s/1dDhasGp
参考资料:
李刚老师的:《疯狂Android讲义》
网上的一篇文章:http://www.360doc.com/content/13/0110/23/11991_259460170.shtml
Android提供的系统服务之--AlarmManager(闹钟服务)
标签:android 系统服务 闹钟服务 service alarmmanager
原文地址:http://blog.csdn.net/coder_pig/article/details/44017039