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的使用的更多相关文章

  1. 服务 IntentService 前台服务 定时后台服务

    Activity public class MainActivity extends ListActivity {     private int intentNumber = 0;     @Ove ...

  2. IntentService+BroadcastReceiver 实现定时任务

    效果图: AlramIntentService package com.example.admin.water; import android.app.AlarmManager;import andr ...

  3. 什么时候用IntentService

    IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...

  4. IntentService

    http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...

  5. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  6. HandlerThread和IntentService

    HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...

  7. android 中IntentService的作用及使用

    IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...

  8. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  9. Android中Services之异步IntentService

    IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1)  它创 ...

随机推荐

  1. BZOJ 1877 晨跑

    http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...

  2. jQuery.trim(str)

    描述: 去掉字符串起始和结尾的空格. jQuery 代码: $.trim(" hello, how are you? "); 结果: "hello, how are yo ...

  3. Linux服务器挂死案例分析

    问题现象: 在linux服务器上运行一个指定的脚本时,就会出现无数个相同进程的,而且不停的产生,杀也杀不掉,最后系统就陷入死循环,无法登陆,只能人工去按机器的电源键才可以.这够崩溃的吧? 问题分析过程 ...

  4. Using HTML5 audio and video

    From:https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video Using HTML5 ...

  5. Longest Valid Parentheses 解答

    Question Given a string containing just the characters '(' and ')', find the length of the longest v ...

  6. TCP快速重传和快速恢复

    当tcp传送一个分组时会设置一个定时器,如果在规定的实际间隔内没有收到ACK分组,那么则重新传输该分组,但是 如果tcp收到三个连续的ACK分组,此时不管是否过超时间隔则重传该分组,具体步骤如下: 1 ...

  7. javascript 继承机制设计思想

    作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_java ...

  8. vps安全设置

    适合新手及才接触VPS的朋友们看一下.主要是关于VPS安全方面相关内容的 禁止ROOT登陆 保证安全性. 使用DDoS deflate简单防攻击. iftop Linux流量监控工具: 每日自己主动备 ...

  9. Android应用程序进程启动过程的源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址: http://blog.csdn.net/luoshengyang/article/details/6747696 Android 应用程序框架层创 ...

  10. 写一个简易web服务器、ASP.NET核心知识(4)--转载

    第一次尝试(V1.0) 1.理论支持 这里主要要说的关于Socket方面的.主要是一个例子,关于Socket如何建立服务端程序的简单的代码. static void Main(string[] arg ...