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. filter 以及 orderBy的使用

    filter用于关键字过滤操作,orderBy用于排序操作,运行界面如下: 点击标题Name与Email实现排序功能,输入框中输入关键字进行过滤,同时实现根据关键字进行过滤后进行排序操作: ng-re ...

  2. 实例解析shell子进程(subshell )

    http://blog.csdn.net/sosodream/article/details/5683515 http://blog.csdn.net/firefoxbug/article/detai ...

  3. Keil C51程序设计中几种精确延时方法

    1 使用定时器/计数器实现精确延时 单片机系统一般常选用11.059 2 MHz.12 MHz或6 MHz晶振.第一种更容易产生各种标准的波特率,后两种的一个机器周期分别为1 μs和2 μs,便于精确 ...

  4. VS下 dllimport与dllexport作用与区别

    我相信写WIN32程序的人,做过DLL,都会很清楚__declspec(dllexport)的作用,它就是为了省掉在DEF文件中手工定义导出哪些函数的一个方法.当然,如果你的DLL里全是C++的类的话 ...

  5. mysql----用户root被删除或忘记root密码的解决方案

    修改文件my.cnf,可用VIM打开,如:sudo vim /etc/my.cnf 在[mysqld]下加上一行: skip-grant-tables 保存文件,然后重启mysqld程序:sudo s ...

  6. 《Java程序员面试笔试宝典》之 什么是AOP

    AOP(Aspect-Oriented Programming,面向切面编程)是对面向对象开发的一种补充,它允许开发人员在不改变原来模型的基础上动态地修改模型从而满足新的需求.例如,在不改变原来业务逻 ...

  7. CRC 模式及实现

    CRC : Cyclic redundancy Check 循环冗余校验 概述参见wiki百科:http://en.wikipedia.org/wiki/Cyclic_redundancy_check ...

  8. 黑马程序员—— Java SE(2)

    ----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...

  9. 不同浏览器对URL最大长度的限制(转)

    1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...

  10. Android开发记录(转)

    一.Android模拟器相关 1. Android模拟器安装 Market 模拟器默认没有安装 Market,看到网上有较为复杂的安装方法,也有1个简单的,试了简单的,在 Android2.2 模拟器 ...