Android杂记

1.一种createAction的方法:Intent intent = new Intent(String intentStrcing);  

 用处:根据接受到的广播,消息,widget点击事件等进行app落地页载入

2.widget的控件点击事件可以绑定一个PeddingIntent对象.一般成熟应用都有自己的widget,点击后的落地页肯定是app某个native或者web页面。

3.计算类似imageView的动画效果时,这样才是得到控件的宽和高:

        vWidth = getWidth() - getPaddingLeft() - getPaddingRight();
	vHeight = getHeight() - getPaddingTop() - getPaddingBottom();
4.处理onTouch事件等Action的ActionKind时,需要判断的是:event.getAction() & MotionEvent.ACTION_MASK这个值的int值。

  getX获得相对于父布局顶端距离,getRawX获得相对于屏幕顶端的绝对距离。

5.总结两个相似问题。
(1).StringBuffer sb =new StringBuffer(char c);
会把char默认转化成整型,调用的方法是 java.lang.StringBuffer.StringBuffer(int capacity)
(2)直接对android中textView赋值。比如 titleTv.setText(1);
会调用setText(int resId),会到R文件中寻找对应的资源,而不是“1”。可以改成setText(1+"");

6.客户端的消息(特指通知栏的消息)有以下两种实现方式:

  (1)根据闹钟机制,固定时段发送广播,在receiver里接收到广播后,去请求服务器拉取消息。
 (2)服务器推送消息,和客户端协定Action。实现方案是:注册一个监听action = android.intent.action.BOOT_COMPLETED的receiver,

   当手机开机启动之后就会接收到这个广播。然后后台去启用一个service,这个service会持续存活,可以自行设置间隔,比如每5分钟去指定服务器访问其消息池。

    两种方案在拿到json数据之后,都是通过Notification进行封装,代码如下:

/**MessageSummary是消息封装类,包含主题,内容等
**/
public static void addMessageNotify(Context context, MessageSummary summary,boolean needVoice) {
		Notification notification = new Notification(R.drawable.android_jd_notification, summary.getTitle(), System.currentTimeMillis());

		notification.number = count;
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_LIGHTS; // 亮灯
		notification.ledARGB = 0x99f0ff00;
		notification.ledOnMS = 300;
		notification.ledOffMS = 2000;
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		int mode = mAudioManager.getRingerMode();
		if (needVoice && mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
			notification.defaults |= Notification.DEFAULT_SOUND; // 声音
		}

		Intent intent = null;
	        intent = createIntent(MODULE_ID_MESSAGE, null);
		//按照1中的方法

		PendingIntent contentIntent = PendingIntent.getBroadcast(context, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(context, summary.getTitle() + " (共" + count + "条)", summary.getContent(), contentIntent);
		getNotificationManager(context).notify(PUSH_MESSAGE_NOTIFY_ID, notification);
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值