Salut les gars je suis face à un problème étrange, quand je GCMintentService dans mon implémenter des applications, Mon application Temps d'appel à Webservice sort, je reçois une réponse Timedout de webservice avec GCMintentService
permission.GCMIntentService Ralentissez Internet sur mon téléphone Android
Lorsque j'ai désactivé cette permission, mon application communique bien avec le serveur Web, je ne comprends pas comment résoudre le problème.
Toute idée ou suggestion très appréciée.
EDIT
fichier Manifest ici
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.citrusbits.peacekeeper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" >
</uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
android:name="com.citrusbits.peacekeeper.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.citrusbits.peacekeeper.permission.C2D_MESSAGE" />
<application
android:name="com.citrusbits.peacekeeper.app.ApplicationController"
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<activity
android:name="com.citrusbits.peacekeeper.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.citrusbits.peacekeeper.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.citrusbits.peacekeeper.ForgotPasswordActivity"
android:label="@string/title_activity_forgot_password"
android:parentActivityName="com.citrusbits.peacekeeper.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.citrusbits.peacekeeper.MainActivity" />
</activity>
<activity
android:name="com.citrusbits.peacekeeper.HomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustNothing" >
</activity>
<activity
android:name="com.citrusbits.peacekeeper.OwnAlertDetailsActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden" >
</activity>
<activity
android:name="com.citrusbits.peacekeeper.AlertDetailsActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden" >
</activity>
<activity
android:name="com.citrusbits.peacekeeper.UsersFragment"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden" >
</activity>
<activity
android:name="com.citrusbits.peacekeeper.GroupFragment"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden" >
</activity>
<receiver
android:name="com.citrusbits.peacekeeper.app.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name="com.citrusbits.peacekeeper.app.GcmIntentService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Android Classe Code
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class ApplicationController extends Application {
/**
* Log or request TAG
*/
static Context context;
public static final String TAG = "VolleyPatterns";
/**
* Global request queue for Volley
*/
private RequestQueue mRequestQueue;
// public static String APID;
String SENDER_ID = "643413423722";
// public static final String EXTRA_MESSAGE = "message";
// public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
private static final String PREFS_NAME = "Preference";
public static SharedPreferences settings;
GoogleCloudMessaging gcm;
AtomicInteger msgId = new AtomicInteger();
SharedPreferences prefs;
public static String regid;
/**
* A singleton instance of the application class for easy access in other
* places
*/
private static ApplicationController sInstance;
@SuppressLint("NewApi")
@Override
public void onCreate() {
super.onCreate();
// initialize the singleton
sInstance = this;
context = this;
if (Build.VERSION.SDK_INT >= 14) {
registerActivityLifecycleCallbacks(new MyLifecycleHandler());
}
settings = getSharedPreferences(PREFS_NAME, 0);
if (servicesOK()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(sInstance);
if (regid.isEmpty()) {
registerInBackground();
}
}
// AirshipConfigOptions options = AirshipConfigOptions
// .loadDefaultOptions(getBaseContext());
// options.developmentAppKey = "Ex4yMwmKRde-kbuxeR2PkA";
// options.developmentAppSecret = "Iy5B_q4_QsWJO-ax0aivgw";
//
// options.gcmSender = "643413423722";
// options.transport = "gcm";
//
// options.inProduction = false; // determines which app key to use
//
// // Take off initializes the services
// UAirship.takeOff(this, options);
// PushManager.enablePush();
// PushManager.shared().setIntentReceiver(IntentReceiver.class);
//
// CustomPushNotificationBuilder nb = new
// CustomPushNotificationBuilder();
//
// nb.layout = R.layout.notificationlayout;
//
// nb.layoutIconDrawableId = R.drawable.icon;
//
// nb.layoutIconId = R.id.icon;
// nb.layoutSubjectId = R.id.subject;
// nb.layoutMessageId = R.id.message;
//
// nb.constantNotificationId = 100;
//
// PushPreferences prefs = PushManager.shared().getPreferences();
// prefs.setSoundEnabled(true);
// prefs.setVibrateEnabled(true);
// //
// APID = settings.getString("token", null);
//
// if (APID == null) {
//
// String apid = PushManager.shared().getAPID();
//
// if (apid == null) {
// new Thread() {
// @Override
// public void run() {
// while (ApplicationController.APID == null) {
// ApplicationController.APID = PushManager.shared()
// .getAPID();
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
// }.start();
// } else {
// APID = apid;
// }
// //
// SharedPreferences.Editor editor = settings.edit();
// editor.putString("token", APID);
// editor.commit();
// }
}
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
// String registrationId = prefs.getString(PROPERTY_REG_ID, "");
String registrationId = settings.getString("token", "");
if (registrationId.isEmpty()) {
return "";
}
// Check if app was updated; if so, it must clear the registration ID
// since the existing regID is not guaranteed to work with the new
// app version.
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
private SharedPreferences getGCMPreferences(Context context) {
// This sample app persists the registration ID in shared preferences,
// but
// how you store the regID in your app is up to you.
return getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
private static int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
private class GetTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
int appVersion = getAppVersion(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("token", regid);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
}
private void registerInBackground() {
new GetTask().execute(null, null, null);
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
// Dialog dialog =
// GooglePlayServicesUtil.getErrorDialog(isAvailable,
// this, 9001);
// dialog.show();
} else {
Toast.makeText(this, "Can't connect to Google Play Services",
Toast.LENGTH_SHORT).show();
}
return false;
}
public static Context getContext() {
return context;
}
/**
* @return ApplicationController singleton instance
*/
public static synchronized ApplicationController getInstance() {
return sInstance;
}
/**
* @return The Volley Request queue, the queue will be created if it is null
*/
public RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be
// created when it is accessed for the first time
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
/**
* Adds the specified request to the global queue, if tag is specified then
* it is used else Default TAG is used.
*
* @param req
* @param tag
*/
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
VolleyLog.d("Adding request to queue: %s", req.getUrl());
getRequestQueue().add(req);
}
/**
* Adds the specified request to the global queue using the Default TAG.
*
* @param req
* @param tag
*/
public <T> void addToRequestQueue(Request<T> req) {
// set the default tag if tag is empty
req.setTag(TAG);
getRequestQueue().add(req);
}
/**
* Cancels all pending requests by the specified TAG, it is important to
* specify a TAG so that the pending/ongoing requests can be cancelled.
*
* @param tag
*/
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
GCMIntentService
package com.citrusbits.peacekeeper.app;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import com.citrusbits.peacekeeper.HomeActivity;
import com.citrusbits.peacekeeper.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
boolean foreground = false;
Context context;
public GcmIntentService() {
super("Some Value here");
context = this;
}
@Override
protected void onHandleIntent(Intent intent) {
if (Build.VERSION.SDK_INT >= 14) {
foreground = MyLifecycleHandler.isApplicationInForeground();
} else {
foreground = ForegroundCheckTask.isAppOnForeground(context);
}
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that
* GCM will be extended in the future with new message types, just
* ignore any message types you're not interested in, or that you
* don't recognize.
*/
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());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification(extras.getString("payload"));
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
SQLiteDatabase db;
String userid = null;
db = openOrCreateDatabase("PeacekeeperDB.db", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Users(user_id VARCHAR, email VARCHAR, name VARCHAR, password VARCHAR, phone VARCHAR, profilepicture VARCHAR, latitude VARCHAR, longitude VARCHAR, location VARCHAR, groupid VARCHAR, groupname VARCHAR, creatorid VARCHAR, alertid VARCHAR, message VARCHAR, alert_time VARCHAR, type VARCHAR, alertcreator_id VARCHAR, status VARCHAR, joingroup VARCHAR, neighbour_id VARCHAR, alert_image VARCHAR);");
Cursor c = db.rawQuery("SELECT * FROM Users", null);
if (c.getCount() > 0) {
c.moveToFirst();
userid = c.getString(0);
c.close();
}
db.close();
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message", msg);
if (foreground) {
if (userid != null) {
startActivity(intent);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("PeaceKeeper")
.setStyle(
new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
// mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("PeaceKeeper")
.setStyle(
new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
if (userid != null) {
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(contentIntent);
}
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
}
BroadCastReciever
package com.citrusbits.peacekeeper.app;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
boolean foreground = false;
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Veuillez poster votre logcat, manifeste et code pertinent. – Eran
poster votre code ou logcat – Boopathi
J'ai posté mon code là. –