android中暂停服务,Android暂停服务,线程,Asynctask?使用postdelayed的处理程序呢?...

该博客讨论了如何在Android中使用服务(Service),线程(Thread),Timer和Asynctask来实现定时任务。当Asynctask返回true时,会发送通知。作者希望在用户点击通知后,服务能等待20秒再执行下一次任务。文中提供了NotifiyService的代码示例,展示了如何使用HandlerThread和postDelayed来控制任务间隔。
摘要由CSDN通过智能技术生成

我有后台服务(Service→Thread→Timer→Asynctask)。 Timer每5秒执行一次Asynctask。如果Asynctask返回true,则发送通知。Android暂停服务,线程,Asynctask?使用postdelayed的处理程序呢?

现在我希望服务在点击通知(意味着我在接下来的20秒内不会再收到另一个通知)后等待20秒。 什么“对象”需要在这里停止?据我所知,暂停Asynctasks并不是一个好主意。所以它可能是服务或线程的权利?使用postdelayed方法的Handler是最佳解决方案吗?

编辑2016年9月3日

public class NotifiyService extends Service {

String savedsa;

boolean value;

protected static final int DEFAULT_TIMEOUT = 5000;

protected static final int EXTENDED_TIMEOUT = 20000;

private HandlerThread mBgThread;

private Handler mBgHandler;

private MyTimerRunnable mRunnable;

@Override

public void onCreate() {

mBgThread = new HandlerThread("MyBgThread");

mBgThread.start();

mBgHandler = new Handler(mBgThread.getLooper(), (Handler.Callback) this);

mRunnable = new MyTimerRunnable();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

SharedPreferences sharedPreferences7 = getSharedPreferences("Prefsa",MODE_WORLD_READABLE);

savedsa = sharedPreferences7.getString("keysa","");

Toast.makeText(NotifiyService.this,getResources().getString(R.string.MonStarted)+ "\n" + savedsa,Toast.LENGTH_LONG).show();

mBgHandler.removeCallbacks(mRunnable);

mBgHandler.postDelayed(mRunnable,EXTENDED_TIMEOUT);

return START_STICKY;

}

@Override

public void onDestroy() {

//super.onDestroy();

mBgHandler.removeCallbacks(mRunnable);

mBgThread.quitSafely();

Toast.makeText(NotifiyService.this,getResources().getString(R.string.MonStopped), Toast.LENGTH_LONG).show();

}

private class MyTimerRunnable implements Runnable{

@Override

public void run() {

while(!value){

try {

URL url = new URL(savedsa);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setRequestMethod("HEAD");

httpURLConnection.setConnectTimeout(3000);

httpURLConnection.setReadTimeout(3000);

httpURLConnection.connect();

value = true;

} catch (MalformedURLException e) {

e.printStackTrace();

value = false;

} catch (ProtocolException e) {

e.printStackTrace();

value = false;

} catch (IOException e) {

e.printStackTrace();

value = false;

}

if(value){

NotificationCompat.Builder builder = new NotificationCompat.Builder(NotifiyService.this);

builder.setSmallIcon(R.drawable.dummy);

Intent intent = new Intent(NotifiyService.this, Main2Activity.class);

intent.setAction(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(NotifiyService.this,0,intent,0);

builder.setContentIntent(pendingIntent);

builder.setLights(Color.YELLOW, 600, 600);

builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dummy));

builder.setContentTitle(getResources().getString(R.string.newNotify));

builder.setContentText(getResources().getString(R.string.newNotify2));

builder.setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(1, builder.build());

}

mBgHandler.postDelayed(this,DEFAULT_TIMEOUT);}

}

}

@Override

public IBinder onBind(Intent intent) {

// TODO: Return the communication channel to the service.

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值