我必须做一个在45秒内与网络服务通信的android服务。但我不能用商业服务或常规服务来做到这一点。系统会在10分钟后自动关闭。我需要帮助。
最佳答案
public class bildirim extends JobService {
int Kul_Id = 0;
String[] sto;
int sayac = 0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public boolean onStartJob(JobParameters params) {
final ipdatabase dbo = new ipdatabase(getBaseContext());
sto = dbo.ipler(getBaseContext());
database db = new database(bildirim.this);
if (db.dbkontrol()) {
List<String> cr = db.veriListele(2);
if (!cr.isEmpty()) {
Kul_Id = Integer.parseInt(cr.get(4));
}
}
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
new arkaplan().execute("http://" + sto[0] + "/Services.asmx/bildirimal?Kul_ID=" + Kul_Id+"&durum=1");
}
},0,30000);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
@Override
public void onDestroy() {
JobSchedulerService.scheduleJob(getApplicationContext());
super.onDestroy();
}
class arkaplan extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
return jsonparser.getdata(params[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject obj = new JSONObject(s);
JSONArray listarray = obj.getJSONArray("list");
JSONObject fistobj;
for (int i = 0; i < listarray.length(); i++) {
fistobj = listarray.getJSONObject(i);
int icon = R.drawable.redwhitefav;
NotificationManagerCompat notifiynm = NotificationManagerCompat.from(bildirim.this);
Notification notifiy = new NotificationCompat.Builder(bildirim.this, CHANNEL_1_ID)
.setSmallIcon(icon)
.setContentTitle(fistobj.getString("Gork_Deger") + ": " + fistobj.getString("Gork_Mstad"))
.setContentText("Tanım: "+ fistobj.getString("Gork_Tanim") + "\n"+ "Tarih: " + fistobj.getString("Gork_Baslangic"))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.build();
notifiynm.notify(sayac, notifiy);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}