【问题标题】:How to check database in a service Android如何在服务 Android 中检查数据库
【发布时间】:2019-04-08 06:11:58
【问题描述】:

我现在正在开发 Android 一个月,但似乎无法找到解决方案。

如果数据库中有任何更改,我想要发送通知,似乎无法在 Google 上找到答案。我想知道的是:我应该在服务上运行 asynctask 吗?还是直接在 MyService 类的 startcommand 上运行 doInBackground 中的代码?我如何实现这一目标?

我尝试将 asynctask 放在服务上并使用 doInBackground 上的代码,但无法使其工作,所以如果你们知道答案,可以发布一些代码来指导我吗?谢谢

【问题讨论】:

  • 您需要为任何人提供更多信息才能解决此问题。您使用的是哪种数据库服务? Room、SQLite、Realm 等。这也应该是用户看到的通知还是只是让您的应用程序做一些工作?

标签: android service android-asynctask


【解决方案1】:

`公共类AlertService扩展服务{ 公共警报服务(){ }

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
    super.onCreate();

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    StringRequest sr = new StringRequest(Request.Method.POST, "http://localhost/ServiceAlarme.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try
                    {
                        JSONObject js=new JSONObject(response);
                        String etat=js.getString("etat");
                        if(etat.equals("1"))
                        {
                            int NOTIFICATION_ID = 12345;
                            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                            Notification n = new Notification.Builder(getApplicationContext()).setContentTitle("SERVICE ALARME").setContentText("Alerte du ALARME du GAZ").setSmallIcon(R.mipmap.bubble).build();
                            n.defaults |= Notification.DEFAULT_SOUND;
                            n.flags |=Notification.FLAG_AUTO_CANCEL;
                            long[] vibrate = { 0, 100, 200, 300 };
                            n.vibrate = vibrate;
                            notif.notify(NOTIFICATION_ID,n);
                        }
                        else
                        {
                        }
                    }
                    catch(Exception e)
                    {
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("d","0");
            return params;
        }
    };
    RequestQueue rq= Volley.newRequestQueue(getApplicationContext());
    rq.add(sr);

    return super.onStartCommand(intent, flags, startId);
}

} `

【讨论】:

  • protected void onStart(){ super.onStart(); startService(new Intent(Inscription.this,AlertService.class)); }
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多