2017-04-25 4 views
0

Androidプラットフォームを使用してチャットアプリケーションを開発しています。私がやろうとしているのは、アプリケーションがonHandleIntent()関数でプッシュ通知を受け取ったときです。アプリケーションはAsyncTask()関数を呼び出します。 AsyncTask()関数はPHPサーバを呼び出し、PHPサーバからデータを取得します。次に、データを取得し、自分の電話機のSQLiteデータベースに挿入します。以下は、私のコードです:Android PushNotification IntentServiceとサーバーデータベースからデータを取得し、sqliteデータベースを更新するAsyncTask関数を呼び出す

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を()を呼び出すことはできませんです。どうすればこの問題を解決できますか?

+1

ここでasyncTaskを使用している理由は、onHandleIntentはすでにバックグラウンドスレッドで実行されています。 –

答えて

1

インテントサービスはすでにバックグラウンドスレッドで実行されているため、内部で呼び出すための非同期タスクは必要ありません。 onHandleIntent()メソッド内のAPIを直接打つことができます。bcsは既にバックグラウンドスレッドにあります。

+0

つまり、IntentService関数内でHttpURLConnectionを実行できますか? –

+0

はい、IntentService内でhttpUrlConnectionを実行することができます –

+0

@Ashrif Ahamadありがとう、私はそれを得る –

関連する問題