<pre name="code" class="java">一定要注意application要继承FrontiaApplication,如果继承的是Application就会报错
<pre name="code" class="java">忘了最重要的了 ,首先应该说是获取AppKey,哈哈哈
分几步:
<pre name="code" class="java">首先在官网下载pushservice-4.4.0.71.jar和libbdpush_V2_2.导入到工程,在libs目录下建三个文件夹armeabi、mips、x86,将libbdpush_V2_2分别放在各个文件夹中(注意:下载的是libbdpush_V2_2而不是libbdpush_V2_0
1、activity调用:/** 加载百度推送 **/// baiduInit();PushManager.startWork(getApplicationContext(),PushConstants.LOGIN_TYPE_API_KEY,Utils.getMetaValue(MainActivity.this, "api_key"));2、新建一个 Utils类,获取API_Key package com.sfdj.salebaby.push;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.location.LocationManager;import android.os.Bundle;import android.preference.PreferenceManager;public class Utils { public static final String TAG = "PushDemoActivity"; public static final String RESPONSE_METHOD = "method"; public static final String RESPONSE_CONTENT = "content"; public static final String RESPONSE_ERRCODE = "errcode"; protected static final String ACTION_LOGIN = "com.baidu.pushdemo.action.LOGIN"; public static final String ACTION_MESSAGE = "com.baiud.pushdemo.action.MESSAGE"; public static final String ACTION_RESPONSE = "bccsclient.action.RESPONSE"; public static final String ACTION_SHOW_MESSAGE = "bccsclient.action.SHOW_MESSAGE"; protected static final String EXTRA_ACCESS_TOKEN = "access_token"; public static final String EXTRA_MESSAGE = "message"; public static String logStringCache = ""; // 获取ApiKey public static String getMetaValue(Context context, String metaKey) { Bundle metaData = null; String apiKey = null; if (context == null || metaKey == null) { return null; } try { ApplicationInfo ai = context.getPackageManager() .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); if (null != ai) { metaData = ai.metaData; } if (null != metaData) { apiKey = metaData.getString(metaKey); } } catch (NameNotFoundException e) { } return apiKey; } // 用share preference来实现是否绑定的�?��。在ionBind且成功时设置true,unBind且成功时设置false public static boolean hasBind(Context context) { SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(context); String flag = sp.getString("bind_flag", ""); if ("ok".equalsIgnoreCase(flag)) { return true; } return false; } public static void setBind(Context context, boolean flag) { String flagStr = "not"; if (flag) { flagStr = "ok"; } SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = sp.edit(); editor.putString("bind_flag", flagStr); editor.commit(); } public static List<String> getTagsList(String originalText) { if (originalText == null || originalText.equals("")) { return null; } List<String> tags = new ArrayList<String>(); int indexOfComma = originalText.indexOf(','); String tag; while (indexOfComma != -1) { tag = originalText.substring(0, indexOfComma); tags.add(tag); originalText = originalText.substring(indexOfComma + 1); indexOfComma = originalText.indexOf(','); } tags.add(originalText); return tags; } public static String getLogText(Context context) { SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(context); return sp.getString("log_text", ""); } public static void setLogText(Context context, String text) { SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = sp.edit(); editor.putString("log_text", text); editor.commit(); } /*** * 判断gps是否�?�� * * **/ public static boolean isGpsEnable(Context mContext) { LocationManager locationManager = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)); return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } }3、加权限: <!-- Push service 运行需要的权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />4、清单注册: <!-- push应用定义消息receiver声明 --> <receiver android:name="com.sfdj.salebaby.push.MyPushMessageReceiver" > <intent-filter> <!-- 接收push消息 --> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <!-- 接收bind,unbind,fetch,delete等反馈消息 --> <action android:name="com.baidu.android.pushservice.action.RECEIVE" /> <action android:name="com.baidu.android.pushservice.action.notification.CLICK" /> </intent-filter> </receiver> <!-- push必须的receviver和service声明 --> <receiver android:name="com.baidu.android.pushservice.PushServiceReceiver" android:process=":bdservice_v1" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="com.baidu.android.pushservice.action.notification.SHOW" /> <action android:name="com.baidu.android.pushservice.action.media.CLICK" /> <!-- 以下四项为可选的action声明,可大大提高service存活率和消息到达速度 --> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> </receiver> <receiver android:name="com.baidu.android.pushservice.RegistrationReceiver" android:process=":bdservice_v1" > <intent-filter> <action android:name="com.baidu.android.pushservice.action.METHOD" /> <action android:name="com.baidu.android.pushservice.action.BIND_SYNC" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <service android:name="com.baidu.android.pushservice.PushService" android:exported="true" android:process=":bdservice_v1" > <intent-filter> <action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" /> </intent-filter> </service> <service android:name="com.baidu.android.pushservice.CommandService" android:exported="true" /> <!-- 在百度开发者中心查询应用的API Key --> <meta-data android:name="api_key" android:value="Iv7liq5MrPiMyuCzWUEqLfqH" /> <!-- push结束 -->