我正在尝试通过jsoup获得一些值。我必须每10秒取一次值,因为每10秒刷新一次值。我的模拟器没有任何问题,但是每隔10或20分钟我的真实设备就会在“ onDestroy”中给我吐司消息。
我搜索了此问题的解决方法Android app working on emulator but not on real device,但ıt不起作用。
我的服务代码:
@Nullable
@Override
public IBinder onBind(Intent ıntent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId ) {
Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
// // TODO: 22.03.2019 Hocam buradan gönderilebilir yalnız boş değer kabul etmez.
timer = new Timer();
timer.schedule(new TimerTask() { //her 60 sn de bir bildirimGonder(); metodu çağırılır.
@Override
public void run() {
new getirHtml().execute();
}
}, 0, 5000);
return START_STICKY;
}
public class getirHtml extends AsyncTask<Void,Void,Void > {
@Override
protected Void doInBackground(Void... voids) {
try {
document = Jsoup.connect(url).get();
h1element = document.select("div[data-id=rdIntBranchDoviz]");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
@Override
public void onDestroy() {
// timer.cancel();
Toast.makeText(this, "The service has stopped. This message came from service class.", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy: " + "worked");
}
public class LocalBinder extends Binder {
public doviz_takip_servis getServerInstance() {
return doviz_takip_servis.this;
}
}
我的活动:
@Override
public void onCreate() {
Intent doviz_takip_servis = new Intent(getApplicationContext(),doviz_takip_servis.class);
startService(doviz_takip_servis);
}
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, doviz_takip_servis.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
};
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
mServer = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
something.about.hatay.dviztakip.doviz_takip_servis.LocalBinder mLocalBinder = (something.about.hatay.dviztakip.doviz_takip_servis.LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
}
};
@Override
protected void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
BroadcastReceiver
{
@Override
public void onReceive(final Context context, Intent ıntent) {
Bundle extras = ıntent.getExtras();
String data = extras.getString("key");
String dataaa = extras.getString("dolar");
if (data != null){
String[] separated = data.split("\\ ");
amerikan_doları_alıs = separated[15] ;
}
if (dataaa != null ){
if (Double.parseDouble(amerikan_doları_alıs) > Double.parseDouble(dataaa)){
// Toast.makeText(context, "" + dataaa, Toast.LENGTH_SHORT).show();
}
}
我的xml:
<service
android:name=".doviz_takip_servis"
android:exported="false"
android:supportsRtl="true"
/>
<receiver android:name=".update">
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我对代码的期望可以用作模拟器。我该怎么办?
答案 0 :(得分:0)
首先;我感谢CommonsWare的提示。
我检查了所有文档 https://developer.android.com/about/versions/oreo/android-8.0-changes
我刚刚将应用插入这样的白名单:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
别忘了添加权限:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>