这是我的服务类
package com.example.com.listener;
import android.app.DownloadManager;
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.example.com.R;
import com.example.com.listener.GCMRegistrationListener;
import com.example.com.util.Constants;
import com.example.com.web.RawRequest;
import com.example.com.web.WebRequest;
public class GCMRegistrationService extends IntentService{
private static final String TAG = "GCMRegistrationService";
private static final String[] TOPICS = {"global"};
public GCMRegistrationService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
sendRegistrationToServer(token,sharedPreferences.getString(Constants.EMAIL_SHARED_PREF,""));
Toast.makeText(this,"This is your token " + token,Toast.LENGTH_LONG).show();
Log.i("token", token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(Constants.GCM_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(Constants.GCM_TOKEN_TO_SERVER, false).apply();
}
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token,String email) {
WebRequest request= new RawRequest(this,Constants.SEND_GCM_TOKEN, Request.Method.POST);
Map<String,String> requestParams= new HashMap<>();
requestParams.put(Constants.GCM_TOKEN_LABEL,token);
requestParams.put(Constants.EMAIL_SHARED_PREF,email);
request.sendRequest(new GCMRegistrationListener(), requestParams, null);
}
}
以下是我用来启动服务的代码:
Intent gcmRegister= new Intent(context,GCMRegistrationService.class);
context.startService(gcmRegister);
清单文件条目是: 清单包是com.example.com
和服务条目是
<service android:name=".listener.GCMRegistrationService" android:exported="false" />
不幸的是我的服务没有运行。请帮忙。
答案 0 :(得分:0)
根据您的要求更改您的数据,在清单文件中的结束活动标记之外允许此服务标记:
SUM
将超类添加到onCreate super.onCreate()并通过onStartCommand更改onStart(加上其超类super.onStartCommand()。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="tfe.rma.ciss.be.Server"/>
</application>
</manifest>
答案 1 :(得分:0)
GCM register()已被弃用。使用InstanceID执行常规GCM注册管理。如下所示,使用google play lib来获取它。
Log.i(TAG, "GCM Registration event is received");
String token;
try {
token = InstanceID.getInstance(context).getToken(context.getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token retrieved from google cloud: " + token);
//
} catch (IOException e) {
// exponential back off
Log.e(TAG, "IO error occurred, going to back off. backoffIndexGCM:" + backoffIndexGCM);
exponentialBackOff.postDelayed(exponentialBackOffGCMRegisterRun, 1000 * backoffIndexGCM);
backoffIndexGCM *= 2;
// should not wait more than 1000 sec in any circumstance.
backoffIndexGCM %= 1000;
}