sdk问题初体验:
问题:
用一个shape.xml文件作为Relative的背景,在华为NCE-AL10中怎么都显示不出来,然而在华为BLA-AL00却可以正常显示,让我百思不得其解,经过多次比对发现
我的shape.xml文件在drawable的v24包下,一查Android 7.0以下的手机无法调用v24中的资源,于是将文件转入drawable就好了
这是第一次遇到版本问题导致的bug,在Android7.0以下的手机还没有完全退出市场的情况下资源还是放在drawable文件夹下比较保险。
listview动态设置高度问题:
一开始会觉得这个会很麻烦要检测什么的但是经过长时间的网搜(其实很快啦)
直接贴代码:
private void getListHeight(ListView mlistview) {
ListAdapter listAdapter = mlistview.getAdapter();
int totalHeight = 0;
if (listAdapter.getCount()<3){
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, mlistview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mlistview.getLayoutParams();
params.height = totalHeight + (mlistview.getDividerHeight() * (listAdapter.getCount() - 1));
mlistview.setLayoutParams(params);
}else{
View listItem = listAdapter.getView(0, null, mlistview);
listItem.measure(0, 0);
ViewGroup.LayoutParams params = mlistview.getLayoutParams();
params.height = listItem.getMeasuredHeight()*3;
mlistview.setLayoutParams(params);
}
}
根据实际情况微调然后调用就好很实用,感谢:
https://blog.csdn.net/qq_34517710/article/details/70242367
极光推送(重要)
虽说有着源码的优势但是还是要总结以下才是自己的
总结下步骤:
1、gradle文件配置依赖
implementation 'cn.jiguang.sdk:jpush:3.1.1'
// 此处以JPush 3.1.1 版本为例。
implementation 'cn.jiguang.sdk:jcore:1.1.9'
然后注册极光的appkey
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "jpushkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
]
2、AndroidMenifest中将权限,和需要注册的类注册
权限:
注意红色方框为当前项目的包名
类和服务:
<!--极光-->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>
<!-- since 3.0.9 Required SDK 核心功能 -->
<provider
android:name="cn.jpush.android.service.DataProvider"
android:authorities="com.example.heartgrass.DataProvider"
android:exported="true"
tools:replace="android:exported" />
<!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
<!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
<service
android:name="cn.jpush.android.service.DaemonService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.DaemonService" />
<category android:name="com.example.heartgrass" />
</intent-filter>
</service>
<!-- since 3.1.0 Required SDK 核心功能 -->
<provider
android:name="cn.jpush.android.service.DownloadProvider"
android:authorities="com.example.heartgrass.DownloadProvider"
android:exported="true" />
<!-- Required SDK核心功能 -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<category android:name="com.example.heartgrass" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- Optional -->
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- Required SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.heartgrass" />
</intent-filter>
</activity>
<!-- SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PopWinActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@style/MyDialogStyle">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.heartgrass" />
</intent-filter>
</activity>
<!-- Required SDK核心功能 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" />
<!-- Required SDK核心功能 -->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<!-- Required since 3.0.7 -->
<!-- 新的tag/alias接口结果返回需要开发者配置一个自定的广播 -->
<!-- 该广播需要继承JPush提供的JPushMessageReceiver类, 并如下新增一个 Intent-Filter -->
<!-- <receiver -->
<!-- android:name="自定义 Receiver" -->
<!-- android:enabled="true" > -->
<!-- <intent-filter> -->
<!-- <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> -->
<!-- <category android:name="com.zpf.workzcb" /> -->
<!-- </intent-filter> -->
<!-- </receiver> -->
<!-- User defined. 用户自定义的广播接收器 -->
<receiver
android:name=".modual.jpush.MyReceiver"
android:enabled="true">
<intent-filter>
<!-- Required 用户注册SDK的intent -->
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<!-- Required 用户接收SDK消息的intent -->
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<!-- Required 用户接收SDK通知栏信息的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<!-- Required 用户打开自定义通知栏的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<!-- 接收网络变化 连接/断开 since 1.6.3 -->
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.example.heartgrass" />
</intent-filter>
</receiver>
<!-- Required. For publish channel feature -->
<!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。 -->
<!-- 例如: -->
<!-- 发到 Google Play 的APK可以设置为 google-play; -->
<!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!-- Required. AppKey copied from Portal -->
<meta-data
android:name="JPUSH_APPKEY"
android:value="jpushkey" />
注意替换jpush的key值和里面的包名换成自己项目的包名
3、自定义一个Service
/**
* 自定义接收器
* <p>
* 如果不定义这个 Receiver,则:
* 1) 默认用户会打开主界面
* 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JIGUANG-Example";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
LogUtils.e(">>>>>>[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
// BadgeUtil.applyBadgeCount(context, 1);
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
LogUtils.e(">>>>>>>[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
LogUtils.e(">>>>>[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
LogUtils.e(">>>>>[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
LogUtils.e(">>>>>[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
processCustomMessage(context,bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
LogUtils.e(">>>>>[Myeiver] 用户点击打开了通知1");
LogUtils.e(">>>>>[Myeiver] 用户点击打开了通知2" + bundle.getString(JPushInterface.EXTRA_EXTRA));
// MainActivity.start(context,0);
//打开自定义的Activity
Intent i = new Intent(context, MainActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
LogUtils.e(">>>>>跳转到订单页" );
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
LogUtils.e(">>>>>[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
LogUtils.e(">>>>>[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
LogUtils.e(">>>>>[MyReceiver] Unhandled intent - " + intent.getAction());
}
} catch (Exception e) {
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
LogUtils.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " + json.optString(myKey) + "]");
}
} catch (JSONException e) {
LogUtils.e(">>>>>Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
// private void processCustomMessage(Context context, Bundle bundle) {
// if (PrivateSecretaryActivity.isForeground) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//
EventBus.getDefault().post(message, "jpush");
// EventBus.getDefault().post(extras, "jpush");
//
//
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
// }
// }
/**
*
* 自定义推送的声音
* @param context
* @param bundle
*/
private void processCustomMessage(Context context, Bundle bundle) {
NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
//这一步必须要有而且setSmallIcon也必须要,没有就会设置自定义声音不成功
// notification.setAutoCancel(true).setSmallIcon(R.mipmap.logo);
String alert = bundle.getString(JPushInterface.EXTRA_ALERT);
if (alert!=null&&!alert.equals("")){
notification.setSound(
Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.message)); }
//最后刷新notification是必须的
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build()); }
}
最后就是在MainAcitivty或者需要的地方init
补充一个动态设置别名或者标签的方法:
String TAG = ">>>>>>";
private static final int MSG_SET_ALIAS = 1001;
//动态设置标签Tab
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_SET_ALIAS:
Log.d(TAG, "Set alias in handler.");
// 调用 JPush 接口来设置标签 。
JPushInterface.setTags(getApplicationContext(), 1 ,
(Set<String>) msg.obj);
// 调用 JPush 接口来设置别名 。
// JPushInterface.setAliasAndTags(getApplicationContext(),
// (String) msg.obj,
// null,
// mAliasCallback);
break;
default:
Log.i(TAG, "Unhandled msg - " + msg.what);
}
}
};private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
@Override
public void gotResult(int code, String alias, Set<String> tags) {
String logs;
switch (code) {
case 0:
logs = "Set tag and alias success";
Log.i(TAG, logs);
// 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后,以后不必再次设置了。
break;
case 6002:
logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
Log.i(TAG, logs);
// 延迟 60 秒来调用 Handler 设置别名
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
break;
default:
logs = "Failed with errorCode = " + code;
Log.e(TAG, logs);
}
}
};
//调用设置标签
Set<String>set=new ArraySet<>();
set.add(“标签”);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS,set));
//调用设置别名:
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS,“别名”));
挺长的博客了,但是这块确实复杂,结束了