我是Android编程的新手,我想让服务可以从服务器获取数据,以及数据是否更新。我的申请会显示通知。 但是,我的应用程序只有在第一次打开时才显示通知,服务正在运行,我认为服务什么都不做。谢谢你的帮助。
这是我的服务代码:
public class ServiceBaru extends Service {
SharedPreferences sharedPreferences3;
SharedPreferences.Editor editor3;
SharedPreferences sharedPreferences2;
SharedPreferences.Editor editor2;
Timer time = new Timer();
String url_baru, url_default, url_pakai;
int idl_modif, ids_modif;
static String idl = "";
static String ids = "";
Context mContext;
public ServiceBaru() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Handler handler = new Handler();
sharedPreferences2 = getSharedPreferences("semua_url", MODE_PRIVATE);
url_baru = sharedPreferences2.getString("url_masuk", "");
sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
editor3 = sharedPreferences3.edit();
url_default = "192.168.1.215";
if(url_baru.equals("")){
url_pakai = url_default;
}
else {
url_pakai = url_baru;
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
//performe the deskred task
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://" + url_pakai + "/www/index.php/ambilid").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//Sengaja Kosong
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//idl Menunjukkan Id Baru
// ids menunjukkan id lama dari shared preferences
sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
editor3 = sharedPreferences3.edit();
idl = response.body().string();
idl_modif = Integer.parseInt(idl);
ids_modif = sharedPreferences3.getInt("idpertama", 0);
if(ids_modif==0) {
ids = idl;
ids_modif = Integer.parseInt(ids);
editor3.putInt("idpertama", ids_modif);
editor3.commit();
}
else{
if(idl_modif>ids_modif){
NotificationCompat.Builder notif = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.logoutragunung).setContentTitle("UTRAPOS Mobile").setContentText("Ada Data Baru Yang Perlu Approve");
Intent notifklik = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentnotif = PendingIntent.getActivity(getApplicationContext(), 0, notifklik, PendingIntent.FLAG_UPDATE_CURRENT);
notif.setContentIntent(contentnotif);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notif.build());
editor3.putInt("idpertama", idl_modif);
editor3.commit();
System.out.println(idl_modif + ids_modif);
}
}
}
});
}
}, 1000);
System.out.println("Hallo Dari Services");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
答案 0 :(得分:1)
handler.postDelayed()
这将只运行您的代码一次。即使你通过在1000毫秒后调用相同的函数来循环它也不会工作。您必须使用JobSheduler定期触发您的任务。