AlarmReceiver 与IntentService的使用
1:GetSmsService.java
public class GetSmsService extends IntentService{
private AlarmManager alarmManager = null;
private PendingIntent alarmIntent = null;
public GetSmsService(){
super("WtacService");
}
public GetSmsService(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);
String ALARM_ACTION = "com.wzh.receiver.ACTION_WTAC_ALAEM";
Intent intentTo = new Intent(ALARM_ACTION);
alarmIntent = PendingIntent.getBroadcast(this, 0, intentTo, 0);
}
@Override
protected void onHandleIntent(Intent intent) {
final Context context = this.getApplicationContext();
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long triggerAtMillis = SystemClock.elapsedRealtime()+(5*1000);
long intervalMillis = 5*1000; //间隔时间
alarmManager.setInexactRepeating(alarmType, triggerAtMillis, intervalMillis, alarmIntent);
if(PhoneInfo.isConnectInternet(context)){
System.out.println("从服务器获取数据:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
Thread.sleep(2*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("进行短信发送:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
}
2:GetSmsAlarmReceiver.java
public class GetSmsAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent startIntent = new Intent(context, GetSmsService.class);
context.startService(startIntent);
}
}
3:MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4:AndroidManifest.xml
<receiver
android:name="com.wzh.service.GetSmsAlarmReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="com.wzh.receiver.ACTION_WTAC_ALAEM" />
</intent-filter>
</receiver> <service
android:name="com.wzh.service.GetSmsService"
android:enabled="true"
android:exported="false" />
AlarmReceiver 与IntentService的使用的更多相关文章
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- IntentService+BroadcastReceiver 实现定时任务
效果图: AlramIntentService package com.example.admin.water; import android.app.AlarmManager;import andr ...
- 什么时候用IntentService
IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...
- IntentService
http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...
- 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的
稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- Android中的IntentService
首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...
- Android中Services之异步IntentService
IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1) 它创 ...
随机推荐
- MySQL库表状态查询
一. 查看库的各链接状态 对于一个mysql连接或者一个线程,任何时刻都有一个状态,表示其当前正在做什么.一般使用show full processlist查看. +---------+------- ...
- 【转】兼容性测试套件(CTS)框架用户手册
原文网址:http://blog.sina.com.cn/s/blog_416166e90102v6bi.html 兼容性测试套件(CTS)框架用户手册 1.为什么需要兼容性测试(以下称CTS)? 2 ...
- 10招搞定web设计风格指南
From:http://www.ui.cn/detail/27579.html 今时今日,网站的创建正变得越来越复杂,而且一般都不是一个人就能干的了的.在创建网站过程中,我们需要保证设计前后一致,并符 ...
- Spiral Matrix 解答
Question Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- Hdu2860-Regroup(种类并查集)
Problem Description When ALPC42 got to a panzer brigade, He was asked to build software to help them ...
- hdu 4336 Card Collector(期望 dp 状态压缩)
Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...
- 最长公共子串LCS(Longest Common Substring)
一.问题描述 寻求两个字符串中的最大公共字串,其中子串是指字符串中连续的字符组成的,而不是像子序列,按照字符的前后顺序组成.如str1="sgabacbadfgbacst",str ...
- Hug the princess(思维,位运算)
Hug the princess Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...