没有合适的资源?快使用搜索试试~ 我知道了~
首页安卓 开启service每分钟执行一次任务 模拟定时 或者定时任务
再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢? 有了Service为什么还要有个IntentService呢? 原因如下: 1)Service默认运行在主线程中,IntentService运行在一个新的线程中 2)Service需要主动调用stopSelf()或stopService()服务才可以停止,IntentService运行完后自动停止 使用IntentService需要注意2点: 1)构造函数中一定要调用父类的有参构造函数 2)需要耗时处理的事情放在onHandleIntent(Intent intent)
资源详情
资源评论
资源推荐

安卓安卓 开启开启service每分钟执行一次任务每分钟执行一次任务 模拟定时模拟定时 或者定时任务或者定时任务
再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢?
有了Service为什么还要有个IntentService呢?
原因如下:
1)Service默认运行在主线程中,IntentService运行在一个新的线程中
2)Service需要主动调用stopSelf()或stopService()服务才可以停止,IntentService运行完后自动停止
使用IntentService需要注意2点:
1)构造函数中一定要调用父类的有参构造函数
2)需要耗时处理的事情放在onHandleIntent(Intent intent) 函数中
代码部分:此处实现的功能是一个在后台每分钟执行一次任务,时间可以修改,可以替代闹钟
MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开启service 执行定时闹钟
Intent intent=new Intent(MainActivity.this, My_Service.class);
startService(intent);
}
}
My_Service:
package youli.com.example.administrator.ysapp_demo.fragment.YAOXIANG.naozhong;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import java.util.ArrayList;
import java.util.Calendar;


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0