Ken

Talk is cheap. Show me the code


  • 首页

  • 标签

  • 分类

  • 归档

  • 关于

  • Sitemap

  • 搜索

Android中Notification的PendingIntent无效问题解决

发表于 2016-03-11 | 分类于 Android |

下面来一段代码,引出今天的梗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent();
intent.setClass(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("aa","bb");
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification mNotification;
if (android.os.Build.VERSION.SDK_INT < 16) &#123;
mNotification = new Notification();
mNotification.icon = R.drawable.ic_launcher;// icon 的id
mNotification.defaults = Notification.DEFAULT_ALL;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
&#125; else &#123;
mNotification = new Notification.Builder(context)
.setContentTitle(title).setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true).setContentIntent(mPendingIntent).build();
&#125;
nm.notify(0, mNotification);

上面的代码就是最基本的消息通知,然后启动相应的界面,现在世面上大多数也都是这么写的,但是最近却栽在这个坑上了,为啥这么说,因为当我们消息推送的时候在其他机型这样写都是OK的,唯独在华为上面,点击消息启动栏没有任何反应,也不闪退,也不给我们答案,就是通知状态没有了。纠结了一阵子,今天终于。。。。
我们先来看一下,作用于此事件的幕后黑手究竟是谁,上面的代码一目了然,PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags),nm.notify(0, mNotification);就是这两个方法在搞怪,我们来看看里面的参数,PendingIntent.getActivity)官方的解释是这样的

Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

主要的意思就是使用PendingIntent来启动一个Activity,就像用Context.startActivity(Intent)来启动一个Activity一样,注意这里的activity将是上下文之外现有的activity,所以你必须使用Intent.FLAG_ACTIVITY_NEW_TASK标记位来启动一个intent。

这里面的第一个,第三个参数这里不打算细说了。

1
2
3
4
5
6
7
8
9
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intent Intent of the activity to be launched.
* @param flags May be &#123;@link #FLAG_ONE_SHOT&#125;, &#123;@link #FLAG_NO_CREATE&#125;,
* &#123;@link #FLAG_CANCEL_CURRENT&#125;, &#123;@link #FLAG_UPDATE_CURRENT&#125;,
* or any of the flags as supported by
* &#123;@link Intent#fillIn Intent.fillIn()&#125; to control which unspecified parts
* of the intent that can be supplied when the actual send happens.

摘自源码,当我们点进去getActivity方法时是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static PendingIntent getActivity(Context context, int requestCode,
Intent intent, @Flags int flags) &#123;
return getActivity(context, requestCode, intent, flags, null);
&#125;
public static PendingIntent getActivity(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags, @Nullable Bundle options) &#123;
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try &#123;
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess();
IIntentSender target =
ActivityManagerNative.getDefault().getIntentSender(
ActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, new Intent[] &#123; intent &#125;,
resolvedType != null ? new String[] &#123; resolvedType &#125; : null,
flags, options, UserHandle.myUserId());
return target != null ? new PendingIntent(target) : null;
&#125; catch (RemoteException e) &#123;
&#125;
return null;
&#125;
阅读全文 »

ScrollView中嵌套滚动控件手势解决

发表于 2016-03-08 | 分类于 Android |

今天做项目时遇到这么一个场景,在一个ScrollView中需要有一个时间选择控件,就是仿的iOS时间选择那种,布局实现很简单很快实现,可是就是在玩的时候,轮子滚动和外层的ScrollView滚动起了一点摩擦。
一上来就想到的是事件冲突了。最终有这么一个简单的方法。

1
2
3
4
5
6
7
8
9
10
11
listView.setOnTouchListener(new View.OnTouchListener() &#123;
@Override
public boolean onTouch(View v, MotionEvent event) &#123;
if (event.getAction() == MotionEvent.ACTION_UP) &#123;
mSrollView.requestDisallowInterceptTouchEvent(false);
&#125; else &#123;
mSrollView.requestDisallowInterceptTouchEvent(true);
&#125;
return false;
&#125;
&#125;);

mSrollView.requestDisallowInterceptTouchEvent()这句话的意思就是告诉父view不要拦截此事件,哥自己来处理就好。
对listview,scrollView,recycleView都适用。

版权声明:



除非注明,本博文章均为原创,转载请以链接形式标明本文地址。

阅读全文 »

面试时候一些问题回答

发表于 2016-03-06 | 分类于 Android |

现在已经就职阿里巴巴-菜鸟的Android工程师马天宇,一个月面试了杭州10几家有名的互联网公司,一次分享,我帮忙整理了一下,整理到了知乎上面,一些架构师才会考虑的面试题,有一些深度,帮大家扩展知识面,但是下面给出的经典答案,大家可以点击查看。都是用心在回答的,很赞。

  1. 整体性能如何探测,有哪方面 什么指标,怎么保证更流畅
  2. 讲讲架构这块,如何解耦,大项目逻辑多怎么办。
  3. android的发展大事件和主要技术发展
  4. avtivity(service)启动流程简述
  5. 动态化的几种方案
  6. 热修复的原理
  7. 网络这块怎么优化
  8. 数据库性能怎么保证
  9. 线程安全怎么保证,异步并发这块你怎么做的

原文链接

知乎欢迎关注😊

版权声明:



除非注明,本博文章均为原创,转载请以链接形式标明本文地址。

阅读全文 »

AndroidSupportLibrary23.2重磅更新

发表于 2016-02-25 | 分类于 Android |
使用Hexo在GitHub上搭建个人博客
阅读全文 »

AndroidStudio设置代理更新

发表于 2016-02-03 | 分类于 Android |

最近AndroidStudio更新频繁,Preview几乎一天一个版本,但是我从3之后就升不上去了,期间更新几次一次Failure,就没管,因为我用的ShadowSocks,应该全局翻墙可以啊。不行就纳闷了,直到今天感谢群里一哥们,使用SS的哥们福音到了,请看这篇文章。

不好意思,把你骗进来了,嘿嘿,看下面的这个Blog应该能完美解决你的问题

AndroidStudio设置代理

亲测成功Update到了Preview9

版权声明:



除非注明,本博文章均为原创,转载请以链接形式标明本文地址。

阅读全文 »

自定义文本显示TextView

发表于 2016-01-31 | 分类于 Android |

现在的app,数据我们从后台拿到,然后显示在指定的区域,但是后台可能会包含一些html的标签,像<a>,<b>等等,我们总不能让用户看到这些内容,我们客户端处理一下就好了,自定义TextView就OK。

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
public class MyCustomTextView extends TextView &#123;
public MyCustomTextView(Context context) &#123;
super(context);
&#125;

public MyCustomTextView(Context context, AttributeSet attrs) &#123;
super(context, attrs);
&#125;

public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) &#123;
super(context, attrs, defStyle);
&#125;

public void set(String comments) &#123;
if (comments.contains("<")) &#123;
setText(Html.fromHtml(comments));
setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = getText();
if (text instanceof Spannable) &#123;
int end = text.length();
Spannable sp = (Spannable) getText();
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(
text);
style.clearSpans();// should clear old spans

// 循环把链接发过去
for (URLSpan url : urls) &#123;
MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
style.setSpan(myURLSpan, sp.getSpanStart(url),
sp.getSpanEnd(url),
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
&#125;
setText(style);
&#125;
&#125; else &#123;
setText(comments);
&#125;
&#125;

class MyURLSpan extends ClickableSpan &#123;

private String mUrl;

MyURLSpan(String url) &#123;
mUrl = url;
&#125;

@Override
public void updateDrawState(TextPaint ds) &#123;
super.updateDrawState(ds);
ds.setColor(Color.parseColor("#507daf"));
ds.setUnderlineText(false); // 去掉下划线
&#125;
//处理链接点击事件
@Override
public void onClick(View widget) &#123;

&#125;
&#125;
&#125;

使用的时候直接在XML中引用就好了,然后java代码中显示我们要显示的内容。MyCustomTextView.set(content);就好了。

版权声明:



除非注明,本博文章均为原创,转载请以链接形式标明本文地址。

阅读全文 »
1234…7
Ken

Ken

Android,BlockChain Developer

38 日志
10 分类
34 标签
RSS
GitHub
Links
  • Go Blockchain
© 2016 — 2018 Ken
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4