編輯:關於android開發
因為業務需要,android客戶端需要加推送,原來采用的百度推送,但是小米手機有時候收不到,後來換成了極光推送,極光的話所有設備都能收到推送,但是在高峰的時候會推遲,博主說的免費版的,收費的沒用過,也不錯評論
博主用的android studio開發的,.so文件放置的位置啥的可能和eclipse有點不同,感覺用android studio開發jni一類的真的好費勁,博主現在老膩歪了,唉,廢話不多說,進入正題:
首先,去極光推送官網注冊一系列流程,不過貌似不用認證,老長時間注冊的了,忘得差不多了,然後左側有添加新應用
開始創建應用,注冊的時候必須把包名寫對,可以直接把清單文件中的包名復制就可了,當然,android studio一套代碼開發多個包的話例外
這裡有兩個key,第一個的話是android客戶端需要配置的,然後就是後台,後台兩個都需要,注意的是包名一旦定義便不能更改,定義的話極光推送會自己校驗的,給出相應的提示,告訴能不能使用。
再有就是將相應的配置到自己的app中,博主這裡直接下載了相應的demo,進行配置,這裡的話可以先運行demo,測試一下
因為這個demo的包名和自己的一樣,所以只能存在一個demo,好,現在開始配置自己的demo
第一步需要創建兩個包,下面用箭頭標注了
這裡吐槽一下,如果用android studio建立的工程更改過名字的話一般配置的話都不會配置成功的,貌似是因為c語言是弱語言的原因,所以說爭取不要配置改過名的工程。
然後是配置lib包
再有就是配置清單文件,最重要的是前面那幾行,還有就是service的intent的具體名稱
這個千萬不要忘了,博主第一次沒注意就給忘了,收不到推送
再有就是配置服務,看過騰訊的飛鴿,感覺飛鴿的比極光要好點,能讓服務的存活率更高,因為飛鴿的不僅用到了服務還用到了廣播重啟服務,而極光的僅僅用來接收推送消息有時間大家可以了解一下
名字和路徑都要對應,再有就是需要初始化的
還有就是一個util,剩下的activity可以根據需要自己定義就可以了
還有就是xml文件,考進去就可以了
反過來看一下極光的demo
剩下的就是需要初始化,就是入口的activity,或者fragmentactivity,否則的話後台沒有辦法統計
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
package
cn.edu.sjzc.student.uiActivity;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.content.IntentFilter;
import
android.content.SharedPreferences;
import
android.content.SharedPreferences.Editor;
import
android.os.Bundle;
import
android.os.Parcelable;
import
android.support.v4.view.PagerAdapter;
import
android.support.v4.view.ViewPager;
import
android.support.v4.view.ViewPager.OnPageChangeListener;
import
android.view.LayoutInflater;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.OnTouchListener;
import
android.view.Window;
import
java.util.ArrayList;
import
java.util.List;
import
cn.edu.sjzc.student.R;
import
cn.edu.sjzc.student.uiFragment.MainTabActivity;
import
cn.edu.sjzc.student.util.JpushUtil;
import
cn.jpush.android.api.JPushInterface;
public
class
GuideActivity
extends
BaseActivity
implements
OnTouchListener {
private
ViewPager viewPager;
private
List listView;
private
List listDots;
private
int
thePos =
0
;
private
int
oldPosition;
private
int
count =
0
;
private
long
firstTime =
0
;
private
long
secondTime =
0
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
JPushInterface.init(
this
);
init();
registerMessageReceiver();
// used for receive msg
if
(isFirstStart()) {
setTarget();
setContentView(R.layout.activity_guide);
initViewPager();
initDots();
}
else
{
Intent it =
new
Intent();
it.setClass(
this
, AppStartActivicy.
class
);
startActivity(it);
finish();
}
}
// 初始化 JPush。如果已經初始化,但沒有登錄成功,則執行重新登錄。
private
void
init() {
JPushInterface.init(getApplicationContext());
}
@Override
protected
void
onResume() {
isForeground =
true
;
super
.onResume();
JPushInterface.onResume(
this
);
}
@Override
protected
void
onPause() {
isForeground =
false
;
super
.onPause();
JPushInterface.onPause(
this
);
}
@Override
protected
void
onDestroy() {
unregisterReceiver(mMessageReceiver);
super
.onDestroy();
}
//for receive customer msg from jpush server
private
MessageReceiver mMessageReceiver;
public
static
final
String MESSAGE_RECEIVED_ACTION =
"cn.edu.sjzc.student.broadcasterceiver.MESSAGE_RECEIVED_ACTION"
;
public
static
final
String KEY_TITLE =
"title"
;
public
static
final
String KEY_MESSAGE =
"message"
;
public
static
final
String KEY_EXTRAS =
"extras"
;
public
void
registerMessageReceiver() {
mMessageReceiver =
new
MessageReceiver();
IntentFilter filter =
new
IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
public
class
MessageReceiver
extends
BroadcastReceiver {
@Override
public
void
onReceive(Context context, Intent intent) {
if
(MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg =
new
StringBuilder();
showMsg.append(KEY_MESSAGE +
" : "
+ messge +
"\n"
);
if
(!JpushUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS +
" : "
+ extras +
"\n"
);
}
// setCostomMsg(showMsg.toString());
}
}
}
/**
*/
private
boolean
isFirstStart() {
SharedPreferences share = getSharedPreferences(
"fs"
, MODE_PRIVATE);
String target = share.getString(
"isfs"
,
"0"
);
if
(target.equals(
"0"
)) {
return
true
;
}
else
{
return
false
;
}
}
/**
*/
private
void
setTarget() {
SharedPreferences share = getSharedPreferences(
"fs"
, MODE_PRIVATE);
Editor editor = share.edit();
editor.putString(
"isfs"
,
"no"
);
editor.commit();
}
private
void
initViewPager() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
listView =
new
ArrayList();
LayoutInflater inflater = getLayoutInflater();
listView.add(inflater.inflate(R.layout.lay1,
null
));
listView.add(inflater.inflate(R.layout.lay2,
null
));
listView.add(inflater.inflate(R.layout.lay3,
null
));
listView.add(inflater.inflate(R.layout.lay4,
null
));
for
(
int
i =
0
; i < listView.size(); i++) {
View view = (View) listView.get(i);
view.setOnTouchListener(
this
);
}
viewPager.setAdapter(
new
MyPagerAdapter(listView));
viewPager.setOnPageChangeListener(
new
MyPagerChangeListener());
}
private
void
initDots() {
listDots =
new
ArrayList();
listDots.add(findViewById(R.id.dot01));
listDots.add(findViewById(R.id.dot02));
listDots.add(findViewById(R.id.dot03));
listDots.add(findViewById(R.id.dot04));
}
public
class
MyPagerChangeListener
implements
OnPageChangeListener {
public
void
onPageScrollStateChanged(
int
arg0) {
// TODO Auto-generated method stub
}
public
void
onPageScrolled(
int
arg0,
float
arg1,
int
arg2) {
// TODO Auto-generated method stub
}
public
void
onPageSelected(
int
position) {
((View) listDots.get(position))
.setBackgroundResource(R.drawable.dot_focused);
((View) listDots.get(oldPosition))
.setBackgroundResource(R.drawable.dot_normal);
oldPosition = position;
thePos = position;
System.out.println(thePos);
}
}
public
class
MyPagerAdapter
extends
PagerAdapter {
private
List list;
public
MyPagerAdapter(List list) {
this
.list = list;
}
@Override
public
void
destroyItem(View view,
int
index, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) view).removeView(list.get(index));
}
@Override
public
void
finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
list.size();
}
@Override
public
Object instantiateItem(View view,
int
index) {
((ViewPager) view).addView(list.get(index),
0
);
return
list.get(index);
}
@Override
public
boolean
isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return
view == (object);
}
@Override
public
void
restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public
Parcelable saveState() {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
void
startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
public
boolean
onTouch(View arg0, MotionEvent event) {
if
(MotionEvent.ACTION_DOWN == event.getAction() && thePos ==
3
) {
count++;
if
(count ==
1
) {
firstTime = System.currentTimeMillis();
}
else
{
secondTime = System.currentTimeMillis();
if
(secondTime - firstTime >
0
) {
Intent it =
new
Intent();
it.setClass(
this
, LoginDemoActivity.
class
);
startActivity(it);
finish();
}
count =
0
;
firstTime =
0
;
secondTime =
0
;
}
}
return
true
;
}
}
一個是通知欄,另一個是log打印
收到的推送用戶可以自定義顯示
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package
cn.edu.sjzc.student.uiActivity;
import
cn.edu.sjzc.student.R;
import
cn.edu.sjzc.student.app.UserApplication;
import
cn.jpush.android.api.JPushInterface;
import
android.app.Activity;
import
android.content.Intent;
import
android.content.SharedPreferences;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup.LayoutParams;
import
android.widget.ImageButton;
import
android.widget.TextView;
public
class
TestActivity
extends
BaseActivity {
private
TextView jpush_title, jpush_content;
private
String title, content, number;
private
ImageButton jpush_back;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initData();
}
private
void
initView() {
TextView tv =
new
TextView(
this
);
tv.setText(
"用戶自定義打開的Activity"
);
Intent intent = getIntent();
if
(
null
!= intent) {
Bundle bundle = getIntent().getExtras();
title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
content = bundle.getString(JPushInterface.EXTRA_ALERT);
}
jpush_back = (ImageButton) findViewById(R.id.jpush_back);
jpush_back.setOnClickListener(
this
);
jpush_title = (TextView) findViewById(R.id.jpush_title);
jpush_content = (TextView) findViewById(R.id.jpush_content);
}
private
void
initData() {
SharedPreferences userdata =
this
.getSharedPreferences(UserApplication.USER_DATA,
0
);
number = userdata.getString(UserApplication.USER_DATA_NUMBER,
""
);
jpush_title.setText(title);
jpush_content.setText(content);
}
@Override
public
void
onClick(View v) {
super
.onClick(v);
switch
(v.getId()) {
case
R.id.jpush_back:
finish();
break
;
}
}
}
基本這樣就配置成功了,剩下的深入的可以看極光的api,寫的很好很詳細自定義View--一個簡單地圓形Progress效果,view--圓形progress先看效果圖吧 我們要實現一個自定義的再一個圓形中繪制一個弧形的自定義View,思路
Android開發3:Intent、Bundle的使用和ListView的應用 、RelativeLayout(相對布局)簡述(簡單通訊錄的實現),relativelay
[android] 天氣app布局練習(二),androidapp主要練習一下GridView MainActivity.java package com.examp
使用SVG圖片格式 SVG格式, 適應屏幕, 圖片較小, 還有很多優點, 參考. 本文講解如何使用SVG格式. SVG: Scalable Vector Graphic