Android IntentService 与Alarm开启任务关闭任务
1:MyService
public class MyService extends IntentService{
AlarmManager alarmManager = null;
PendingIntent alarmIntent = null; public MyService(){
super("MyService");
} public MyService(String name){
super(name);
} @Override
public IBinder onBind(Intent arg0) {
return null;
} @Override
public void onCreate() {
super.onCreate();
alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intentTo = new Intent("com.yan.receive.MY_ALARM");
alarmIntent = PendingIntent.getBroadcast(this, 0, intentTo, 0);
} @Override
protected void onHandleIntent(Intent arg0) {
//final Context context = this.getApplicationContext(); int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long triggerAtMillis = SystemClock.elapsedRealtime()+(5*1000);//执行间隔时间5s
long intervalMillis = 2*1000;
alarmManager.setInexactRepeating(alarmType, triggerAtMillis, intervalMillis, alarmIntent); System.out.println("============执行============");
}
}
2:MyAlarmReceiver
public class MyAlarmReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent arg1) {
Intent startIntent = new Intent(context, MyService.class);
context.startService(startIntent);
} }
3:MainActivity
public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
Button btnStart;
Button btnClose;
AlarmManager alarmManager = null;
PendingIntent alarmIntent = null; public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false); btnStart = (Button)rootView.findViewById(R.id.btnStart);
btnClose = (Button)rootView.findViewById(R.id.btnClose); btnStart.setOnClickListener(btnClickLIstener());
btnClose.setOnClickListener(btnClickLIstener()); return rootView;
} OnClickListener btnClickLIstener(){
return new OnClickListener(){
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.btnStart:
// 启动服务
Intent myIntent = new Intent(getActivity(), MyService.class);
getActivity().startService(myIntent);
System.out.println("=========启动服务");
break;
case R.id.btnClose:
alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intentTo = new Intent("com.yan.receive.MY_ALARM");
alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intentTo, 0);
if(null != alarmIntent){
alarmManager.cancel(alarmIntent);
}
System.out.println("=========停止Alarm");
getActivity().stopService(new Intent(getActivity(), MyService.class));
System.out.println("=========停止服务");
break;
}
}
};
}
}
}
4:activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.intentservice.MainActivity"
tools:ignore="MergeRootFrame" />
5:fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.intentservice.MainActivity$PlaceholderFragment" > <Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启任务" /> <Button
android:layout_below="@id/btnStart"
android:id="@+id/btnClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭任务" />
</RelativeLayout>
6:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentservice"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <receiver
android:name="com.yan.receive.MyAlarmReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="com.yan.receive.MY_ALARM" />
</intent-filter>
</receiver> <service
android:name="com.yan.service.MyService"
android:enabled="true"
android:exported="false" /> <activity
android:name="com.example.intentservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
Android IntentService 与Alarm开启任务关闭任务的更多相关文章
- Android中如何监听GPS开启和关闭
转自 chenming 原文 Android中如何监听GPS开启和关闭 摘要: 本文简单总结了如何监听GPS开关的小技巧 有时需要监听GPS的开关(这种需求并不多见).实现的思路是监听代表 GPS ...
- 实现开启和关闭android移动网络(转)
开启和关闭移动数据网络有两种方法:一种是通过操作系统的数据库改变APN(网络接入点),从而实现开启和关闭移动数据网络,另一种是通过反射调用系统(ConnectivityManager)的setMobl ...
- Android的WiFi开启与关闭
注意:要首先注册开启和关闭WiFi的权限, <?xml version="1.0" encoding="utf-8"?> <manifest ...
- Android手动控制软键盘的开启和关闭,判断软键盘是否显示;
工具类,拿走就能用: import android.annotation.TargetApi; import android.app.Activity; import android.content. ...
- Android网络开启、关闭整理
package com.my.device_admin.business; import java.lang.reflect.Method; import android.content.Contex ...
- [Android Traffic] Android网络开启、关闭整理
转载: http://blog.csdn.net/tu_bingbing/article/details/8469871 近段时间由于要对手机网络状况进行判断.开启和关闭,从网上找了些资料,现整理如下 ...
- Android 怎样开启与关闭adb 的认证机制(google adb secure) (adb RSA 指纹认证)
前言 欢迎大家我分享和推荐好用的代码段~~声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- Android 开启与关闭软键盘
http://www.cnblogs.com/weixing/p/3300908.html InputMethodManager imm = (InputMethodManager)getSystem ...
- 【转】Android:Bluetooth 的打开和关闭--不错
原文网址:http://www.ifeegoo.com/android-turn-on-and-turn-off-bluetooth.html 摘要:Android 中打开和关闭 Bluetooth ...
随机推荐
- Keil C51 知识点
第一节 Keil C51扩展关键字 深入理解并应用C51对标准ANSIC的扩展是学习C51的关键之一.因为大多数扩展功能都是直接针对8051系列CPU硬件的.大致有以下8类: 8051存储类型 ...
- C++的ABI真特么是evil
果然有些公司明确禁止使用STL也是有一定道理的.其实这个问题的本质就是认为大部分开发者是蠢货,没水平掌控这些细节,项目Release万一出乱子了怎么办?为此吐个槽,我链接一个库时,由于编译参数和链接参 ...
- Linux上的设备管理器
一般windows上我们用它自带的“设备管理器”来查看,管理,安装,卸载驱动. 那么问题来了,Linux上用什么命令来看呢? 可以用: lshw lsusb lspci lsmod 查看特定模块. ...
- java设计模式--结构型模式--适配器模式
适配器模式 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适用性 1.你想使用一个已经存在的类,而它的接口不符合你的需 ...
- 关于qt学习的一点小记录(2)
嗯...这次接了个单 要求图形界面,刚好可以巩固并学习下QT.毫不犹豫的就接了 下面记录下出现的问题: 1. QWidget和QDialog QDialog下的槽函数有accept()与reject( ...
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- android 缓存Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...
- TextView总结
先看看TextView的类继承结构: java.lang.Object ↳ android.view.View ↳ android.widget.TextView 是直接继承View类的,可扩展性很强 ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...