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. sqlplus 一次奇葩问题 HTTP proxy setting has incorrect value

    y@y:~$ sqlplus Error 46 initializing SQL*PlusHTTP proxy setting has incorrect valueSP2-1502: The HTT ...

  2. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

  3. 单片机(MCU)使用常用名字解释

    总线:指能为多个部件服务的信息传送线,在微机系统中各个部件通过总线相互通信. 地址总线(AB):地址总线是单向的,用于传送地址信息.地址总线的宽度为16位,因此基外部存储器直接寻址64K,16位地址总 ...

  4. WebApi 自定义过滤器实现支持AJAX跨域的请求

    我想关于此类话题的文章,大家一搜铺天盖地都是,我写此文的目的,只是对自己学习过程的记录,能对需要的朋友有所帮助,也百感荣幸!!!废话不多说,直接上代码! 客户端:很简单的AJAX请求 <html ...

  5. android UI之Shape详解_GradientDrawable

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大. 那 ...

  6. js深入研究之匿名函数

    /* 匿名函数*/ (function() { var foo = 10; var bar = 2; alert(foo * bar);})(); /* 匿名函数,带参数 */ (function(f ...

  7. windows nfs server for linux

    摘要 在开发嵌入式系统的过程中,为了方便调试与文件共享,需要使用到nfs,即网络文件系统,这位板子的调试测试带来了很大的方便.之前在linux系统下开发,与ARM11核心板 linux系统对接共享也比 ...

  8. Encode and Decode Strings 解答

    Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  9. 学习php常用算法

    <?php /*学用php算法*/ /*1.冒泡法 *思路分析:在要排序的一组数中,对当前还未排好的序列, *从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. *即,每 ...

  10. PHP二分查找(递归和循环)

    二分查找可以通过递归和循环来实现, 思路如下: 将要查找的数和中间数进行比较, 如果相等,则表示找到,返回下标 如果要查找的数小于中间这个数,则说明要查找的数分布在数组左边,修改right边界,使其等 ...