編輯:關於android開發
最近在看推送方面的知識,用的是信鴿推送主要是因為後台用的是信鴿
推送用第三方推送,也就是在客戶端建一個廣播接收器,當服務器發送消息時發送到信鴿,信鴿再發送一次,廣播接受器接受下;
我實現的功能比較簡單,當app在運行狀態時,會在主頁展示一個彈窗展示推送的消息;如果app不在運行狀態且service沒被銷毀就展示默認的通知
那麼如何在主頁展示彈窗:當廣播接受器收到我要的消息時,用觀察者模式,收到消息在發送個消息個主界面
官方的Demo連接:http://xg.qq.com/xg/help/ctr_help/download
修改後Demo的地址:http://download.csdn.net/detail/qq_29774291/9635735
1.先按照官網的例子添加好權限及服務有些服務要改成自己的包名;
2.把官網的廣播接受器先復制到自己的項目中,再在清單文件中添加;
3.隨後添加從官網給出的3個AccessKey了
<meta-data android:name="com.tencent.rdm.uuid" android:value="eb5fa555d70c3246a4944f55be8c266b" /> <!-- 【必須】 請將YOUR_ACCESS_ID修改為APP的AccessId,“21”開頭的10位數字,中間沒空格 --> <!-- 【必須】 請修改為APP的AccessId,“21”開頭的10位數字,中間沒空格 --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="2100219302" /> <!-- 【必須】 請修改為APP的AccessKey,“A”開頭的12位字符串,中間沒空格 --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="A15KJ71W9ELC" />
4.現在在主界面中注冊信鴿推送主要是獲取一個token,然後可以上傳給服務端,現在服務器就可以給你發送消息了;當然從信鴿官方後台可以發送消息,但是從信鴿後台發送的消息有時會接受不到,這點做的完全不及極光推送好
XGPushConfig.enableDebug(this, true);這句發布的時候記得改為false或刪除
1 //信鴿start 2 private String token; 3 private Message message = null; 4 private void XGInit() { 5 // TODO Auto-generated method stub 6 XGPushConfig.enableDebug(this, true); 7 // 如果需要知道注冊是否成功,請使用registerPush(getApplicationContext(), 8 // XGIOperateCallback)帶callback版本 9 // 如果需要綁定賬號,請使用registerPush(getApplicationContext(),account)版本 10 // 具體可參考詳細的開發指南 11 // 傳遞的參數為ApplicationContext 12 //Context context = getApplicationContext(); 13 //1.獲取設備的Token 14 Handler handler = new HandlerExtension(MainActivity.this); 15 message = handler.obtainMessage(); 16 XGPushManager.registerPush(getApplicationContext(), new XGIOperateCallback() { 17 18 @Override 19 public void onSuccess(Object data, int flag) { 20 // TODO Auto-generated method stub 21 Log.d("jiejie", "+++ register push sucess. token:" + data + " " + flag); 22 token = data + ""; 23 message.obj = "+++ register push sucess. token:" + data; 24 System.out.println(token); 25 message.sendToTarget(); 26 } 27 28 @Override 29 public void onFail(Object data, int errCode, String msg) { 30 // TODO Auto-generated method stub 31 Log.d("jiejie", "+++ register push fail. token:" + data 32 + ", errCode:" + errCode + ",msg:" 33 + msg); 34 message.obj = "+++ register push fail. token:" + data 35 + ", errCode:" + errCode + ",msg:" + msg; 36 message.sendToTarget(); 37 } 38 }); 39 } 40 private static class HandlerExtension extends Handler{ 41 WeakReference<MainActivity> mActivity; 42 HandlerExtension(MainActivity activity) { 43 mActivity = new WeakReference<MainActivity>(activity); 44 } 45 @Override 46 public void handleMessage(Message msg) { 47 // TODO Auto-generated method stub 48 super.handleMessage(msg); 49 MainActivity theActivity = mActivity.get(); 50 if(theActivity == null){ 51 theActivity = new MainActivity(); 52 } 53 if(msg != null){ 54 Log.d(Constants.LogTag, msg.obj.toString()); 55 System.out.println("ddd"+msg.obj.toString()); 56 } 57 } 58 }
5.修改廣播接受器中的onTextMessage方法,當消息發送來時,將消息發送到主界面,不展示通知
不過你先要知道你app是否正在運行狀態
1 /** 2 * 判斷是否運行在前台 3 * 4 * @param context 5 * @return 6 */ 7 public static boolean isRunningForeground(Context context) { 8 String packageName = getPackageName(context); 9 String topActivityClassName = getTopActivityName(context); 10 Log.d("TAG", "packageName=" + packageName + ",topActivityClassName=" + topActivityClassName); 11 if (packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName)) { 12 Log.d("TAG", "---> isRunningForeGround"); 13 return true; 14 } else { 15 Log.d("TAG", "---> isRunningBackGround"); 16 return false; 17 } 18 } 19 20 // 方法2、通過RunningAppProcessInfo類判斷(不需要額外權限): 21 22 public static boolean isBackground(Context context) { 23 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 24 List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); 25 for (RunningAppProcessInfo appProcess : appProcesses) { 26 if (appProcess.processName.equals(context.getPackageName())) { 27 if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) { 28 Log.i("後台", appProcess.processName); 29 return true; 30 } else { 31 Log.i("前台", appProcess.processName); 32 return false; 33 } 34 } 35 } 36 return false; 37 }
6.通知EventBus來實現觀察者模式在廣播接收器中發送消息
1 // 消息透傳 2 @Override 3 public void onTextMessage(Context context, XGPushTextMessage message) { 4 String text = "收到消息:" + message.toString(); 5 EventBus.getDefault().post(text); 6 System.out.println(text); 7 // 獲取自定義key-value 8 PushTextMessage pushTextMessage = new PushTextMessage(); 9 10 String title = message.getTitle(); 11 String content = message.getContent(); 12 pushTextMessage.setTitle(title); 13 pushTextMessage.setContent(content); 14 String customContent = message.getCustomContent(); 15 if (customContent != null && customContent.length() != 0) { 16 try { 17 // JSONObject obj = new JSONObject(customContent); 18 // // key1為前台配置的key 19 // if (!obj.isNull("key")) { 20 // String value = obj.getString("key"); 21 // LogUtils.log(LogTag, "get custom value:" + value); 22 // } 23 CustomContent custom = com.alibaba.fastjson.JSONObject.parseObject(customContent, CustomContent.class); 24 if (custom != null) { 25 pushTextMessage.setCustomContent(custom); 26 } 27 28 // ... 29 } catch (Exception e) { 30 System.out.println(e + "d"); 31 e.printStackTrace(); 32 } 33 } 34 show(context, text); 35 Log.d("jiejie", "pushTextMessage:" + pushTextMessage); 36 // EventBus.getDefault().post(pushTextMessage); 37 try { 38 // APP自主處理消息的過程... 39 boolean isForeground = AppUtil.isRunningForeground(context); 40 Log.d("jiejie", isForeground + "d"); 41 if (isForeground) { 42 EventBus.getDefault().post(pushTextMessage); 43 } else { 44 notify(context, title, content); 45 } 46 } catch (Exception e) { 47 System.out.println(e + "ddd"); 48 e.printStackTrace(); 49 }
7.在主界面中接受EventBus發送的消息,展示一個彈窗
1 @Subscribe 2 public void onMessageReviced(final PushTextMessage pushTextMessage){ 3 Log.d("jiejie", "好" + " 標題" +pushTextMessage.getTitle() + " 內容:" +pushTextMessage.getContent() + "CustomContent" + pushTextMessage.getCustomContent().getCmd()); 4 if(pushTextMessage != null){ 5 showAlertDialog(this, pushTextMessage); 6 } 7 } 8 private void showAlertDialog(Context context,PushTextMessage text){ 9 AlertDialog.Builder dialog = new AlertDialog.Builder(context); 10 dialog.setTitle("推送的標題"); 11 dialog.setMessage(text.getContent()); 12 dialog.setPositiveButton("確認", new DialogInterface.OnClickListener() { 13 14 @Override 15 public void onClick(DialogInterface arg0, int arg1) { 16 // TODO Auto-generated method stub 17 18 } 19 }); 20 AlertDialog mDialog = dialog.create(); 21 mDialog.show(); 22 } 23 24 @Override 25 protected void onDestroy() { 26 // TODO Auto-generated method stub 27 super.onDestroy(); 28 EventBus.getDefault().unregister(this); 29 }
自定義控件之創建可以復用的組合控件(三) 前面已學習了兩種自定義控件的實現,分別是自定義控件之對現有控件拓展(一)和 自定義控件之直接繼承View創建全新視圖(二),
UI-初識君面之理論篇,ui-初識理論篇 一個好的app不光要用好的功能,還要有好的界面,這樣內外兼修才算得上
Android工程師進階之路 :《Android開發進階:從小工到專家》上市啦! 封面 目錄1 目錄2
HBase Thrift2 CPU過高問題分析HBase Thrift2 CPU過高問題分析.pdf目錄目錄11.現象描述12.問題定位23.解決方案54.相關代碼51.