日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/3/21 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中暂停服务,Android暂停服务,线程,Asynctask?使用postdelayed的处理程序呢?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有后臺服務(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;

}

}

總結

以上是生活随笔為你收集整理的android中暂停服务,Android暂停服务,线程,Asynctask?使用postdelayed的处理程序呢?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。