Mal*_*alo 4 java multithreading android
嗨,我需要每 4 秒调用一次方法,即使设备处于睡眠状态,我也使用带有服务 Start_stick 的警报管理器,服务名称是 TransactionService。当设备处于活动状态并且每精确 4 秒调用一次该方法时,代码运行良好,但是当屏幕锁定且设备休眠时,调用变得不准确。所以现在每 2 秒调用一次该方法,有时每 1 秒调用一次,5 ....
这就是我每 4 秒运行线程以调用方法的方式
AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(
Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(),
TransactionService.class);
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(), 0, notificationIntent, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 4000, pendingIntent);
Run Code Online (Sandbox Code Playgroud)
这是当设备处于活动状态且屏幕处于开启状态时调用该方法的日志
12-30 13:23:00.565 17397-17479/com.ids.simcardrefill D/url: calling
12-30 13:23:04.565 17397-17537/com.ids.simcardrefill D/url:calling
12-30 13:23:08.565 17397-17411/com.ids.simcardrefill D/url:calling
12-30 13:23:12.565 17397-17655/com.ids.simcardrefill D/url:calling
Run Code Online (Sandbox Code Playgroud)
这就是设备休眠时方法的调用方式
12-30 13:09:12.565 17397-17655/com.ids.simcardrefill D/url:calling
12-30 13:09:17.785 17397-17598/com.ids.simcardrefill D/url:calling
12-30 13:09:20.565 17397-17479/com.ids.simcardrefill D/url:calling
12-30 13:09:25.775 17397-17537/com.ids.simcardrefill D/url:calling
12-30 13:09:28.565 17397-17411/com.ids.simcardrefill D/url:calling
Run Code Online (Sandbox Code Playgroud)
这里调用之间的区别是不准确的:2秒、5秒、3秒
这是服务的样子:
public int onStartCommand(Intent intent, int flags, int startId) {
mshared = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
edit = mshared.edit();
hostname = mshared.getString(
getApplicationContext().getString(R.string.hostname), "0");
contin = true;
cost
= mshared.getString(getString(R.string.test), "0.09");
if (contin) {
getTransactions get = new getTransactions(getApplicationContext());
get.execute(hostname);
}
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
`
任何解决方案?
您应该创建一个在后台工作的服务:https : //developer.android.com/guide/components/services.html
您应该使用Handler以实现every 4 second功能。
Handler handler = new Handler();
Runnable test = new Runnable() {
@Override
public void run() {
//do work
handler.post(test, 4000); //wait 4 sec and run again
}
};
public void stopTest() {
handler.removeCallbacks(test);
}
public void startTest() {
handler.post(test,0); //wait 0 ms and run
}
Run Code Online (Sandbox Code Playgroud)
编辑:我已经尝试了下面的代码,它对我有用
我的服务
public class MyService extends Service {
Handler handler;
Runnable test;
public MyService() {
handler = new Handler();
test = new Runnable() {
@Override
public void run() {
Log.d("foo", "bar");
handler.postDelayed(test, 100); //100 ms you should do it 4000
}
};
handler.postDelayed(test, 0);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
Run Code Online (Sandbox Code Playgroud)
主活动.java
@Override
protected void onCreate(Bundle savedInstanceState) {
//some code
startService(new Intent(this, MyService.class));
}
Run Code Online (Sandbox Code Playgroud)
请记住,如果您想要启动-停止功能,请参考我的第一个示例。
| 归档时间: |
|
| 查看次数: |
7887 次 |
| 最近记录: |