1. 程式人生 > >百度推送 ,終於整理完整了

百度推送 ,終於整理完整了

<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結束 -->