需要更改服务运行的时间间隔,即服务已经运行的时间唤醒并开始运行服务本身。我无法做到,因为在我的服务类中有一个激活服务后 ServiseExample 更多的错过以某种方式阻止它并开始使用新参数编织。怎么做? 这是我的主要课程:
public class ServiceExample extends Service {
public static Cursor c;
public static int INTERVAL =1000;
// 60 min = 3600000
public static final int FIRST_RUN = 5; // 5 seconds
int REQUEST_CODE = 11223344;
private RepeatingAlarmService mSMSreceiver;
private IntentFilter mIntentFilter;
static AlarmManager alarmManager;
@Override
public void onCreate() {
super.onCreate();
mSMSreceiver = new RepeatingAlarmService();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
startService(INTERVAL);
Log.v(this.getClass().getName(), "onCreate(..)");
}
@Override
public IBinder onBind(Intent intent) {
Log.v(this.getClass().getName(), "onBind(..)");
return null;
}
@Override
public void onDestroy() {
unregisterReceiver(mSMSreceiver);
if (alarmManager != null) {
Intent intent = new Intent(this, RepeatingAlarmService.class);
alarmManager.cancel(PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0));
}
Toast.makeText(this, "Service Stopped!", Toast.LENGTH_LONG).show();
Log.v(this.getClass().getName(), "Service onDestroy(). Stop AlarmManager at " + new
java.sql.Timestamp(System.currentTimeMillis()).toString());
}
void startService(int interval) {
Intent intent = new Intent(this, RepeatingAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + FIRST_RUN,
interval,
pendingIntent);
Toast.makeText(this, "Service Started.", Toast.LENGTH_LONG).show();
Log.v(this.getClass().getName(), "AlarmManger started at " + new
java.sql.Timestamp(System.currentTimeMillis()).toString());
}
}
以下是在一段时间后进入我服务的课程:
public class RepeatingAlarmService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = connMgr.getActiveNetworkInfo();
android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(( (wifi.isAvailable() || mobile.isAvailable()) && nInfo != null && nInfo.isConnected() ))
{
Toast.makeText(context,"Error Send" + e,Toast.LENGTH_LONG).show();
}
} else {
// CHANGE INTERVAL HERE
Toast.makeText(context,"Connection False or interval is not good",Toast.LENGTH_LONG).show();
}
}
抱歉我的英文。
答案 0 :(得分:0)
我觉得在处理程序中将你的工作放到seprate进程中是很好的,然后你可以轻松地完成它:
private int x = 1000; // this must be declared in your class body not in its methods;
final Handler hand = new Handler();
x = 1000;
hand.post(new Runnable(){
@override
public void run(){
Toast.makeText(getApplicationContext(),"something to tell",1).show();
if(x == 1000){
x = 2000;
}else{
x = 1000;
}
hand.postDelayed(this,x);
}
}
我在我的应用程序服务中做了什么,当我想要间歇性地执行任务时,它就像一个魅力。 试试!!
答案 1 :(得分:0)
要更改重复间隔,只需使用新间隔再次执行此代码:
Intent intent = new Intent(this, RepeatingAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + interval,
interval,
pendingIntent);
当您致电setRepeating()
时,如果已经设置了匹配Intent
的闹钟,则在设置新闹钟之前该闹钟将被取消。