Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库
Posted
技术标签:
【中文标题】Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库【英文标题】:Android PushNotification IntentService and call AsyncTask function to get Data from server database and update sqlite database 【发布时间】:2017-09-22 07:18:03 【问题描述】:我正在使用 android 平台开发一个聊天应用程序。我要做的是当应用程序在 onHandleIntent() 函数中收到推送通知时。应用程序将调用 AsyncTask() 函数。 AsyncTask() 函数调用 php 服务器并从 PHP 服务器获取数据。接下来,我将获取数据并插入手机中的 SQLite 数据库。以下是我的代码:
public class GCMNotificationIntentService extends IntentService
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService()
super("GcmIntentService");
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent)
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty())
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
sendNotification("Send error: " + extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
sendNotification("Deleted messages on server: " + extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
for (int i = 0; i < 3; i++)
Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
try
Thread.sleep(5000);
catch (InterruptedException e)
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification("Message Received from Google GCM Server: " + extras.getString("message"));
Log.i(TAG, "Received: " + extras.toString());
new GetContacts().execute();
GcmBroadcastReceiver.completeWakefulIntent(intent);
private void sendNotification(String msg)
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.gcm_logo)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
private class GetContacts extends AsyncTask<Void, Void, Void>
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected Void doInBackground(Void... arg0)
HttpHandler sh = new HttpHandler();
String url = "http://131.4.44.69/JSON/index.php";
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null)
try
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("contacts");
for (int i = 0; i < contacts.length(); i++)
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
String address = c.getString("address");
String gender = c.getString("gender");
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
HashMap<String, String> contact = new HashMap<>();
contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("mobile", mobile);
catch (JSONException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
但问题是我无法在 IntentService 类中调用 AsyncTask()。我该如何解决这个问题?
【问题讨论】:
为什么你在这里使用 asyncTask,onHandleIntent 已经在后台线程中执行了。 【参考方案1】:Intent 服务已在后台线程中运行,您无需在其中调用任何异步任务。您可以直接在 onHandleIntent() 方法中点击 api,因为它已经在后台线程中
【讨论】:
这意味着我可以在 IntentService 函数中执行 HttpURLConnection 吗? 是的,你可以直接在 IntentService 中做 httpUrlConnection @Ashrif Ahamad 谢谢,我明白了以上是关于Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库的主要内容,如果未能解决你的问题,请参考以下文章
为什么我不能注册我的Android设备? xam.pushnotification
Android GCM PushNotification - 在应用程序中添加添加自定义声音文件
Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库