赞
踩
我有后台服务(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;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。