程序人生
chenfeng0104 · 共 154 篇文章
深入分析AIDL原理
在上一篇文章(Service使用方式)中,介绍了Android进程间通信(IPC)的使用,并给出了一个示例。但并没有深入分析aidl是怎样可以做到进程间通信的,它的执行过程是怎样的? 这篇文章来分析IRemoteService.aidl的执行过程,并理解aidl是怎样跨进程通信的。 当我们创建IRemoteService.aidl文件时,IDE会为我们在gen目录中创建相应的文件。 /** This file is auto-generated. DO NOT MODIFY. * Original file: F:\\workspace\\AndroidImprove\\src\\com\\…
2011-11-13 16:14 · Android · 评论(0)
Service使用方式
使用Service的场合: 1.一个或多个Activity需要向同一应用中的Service发出执行某一操作的命令。 PS:不需要绑定 2.某个Activity需要同一应用中的Service为其单独服务,当此Activity消毁时,也将为其服务的Service一并消毁。 PS:需要绑定 3.多个Activity绑定到同一个Service,仅当所有Activity取消了绑定,Service才消毁。 PS:只有第一个Activity绑定时才调用onBind()方法,第二个、第三个....第N个直接取得第一个Activity绑定时onBind()方法返回的IBinder对象 4.即使退出当前应用时,…
2011-11-13 15:23 · Android · 评论(3)
在onCreate方法中获取某个View的宽度和高度
有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息,而直接调用View组件的getWidth()、getHeight()、getMeasuredWidth()、getMeasuredHeight()、getTop()、getLeft()等方法是无法获取到真实值的,只会得到0。这是因为View组件布局要在onResume回调后完成。下面提供实现方法,onGlobalLayout回调会在布局完成时自动调用 final LinearLayout imageViewContainer = (LinearLayout)findViewById(R.id.crop_photo_im…
2011-10-08 14:54 · Android · 评论(5)
不加载图片,仅仅获取图片文件分辨率
当从SD卡中获取图片时,如果图片太大,加载会出现溢出异常。因此,需要先获取到分辨率,再对大图片按分辨率比率缩小后加载,这样就能够防止溢出异常。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);//没有加载图片,仅获取到图片分辨率 Log.i("CDH", "bitmap equals null? " + (bitmap == nu…
2011-10-08 11:09 · Android · 评论(0)
图片移动和缩放
import android.graphics.Matrix; import android.graphics.PointF; import android.util.FloatMath; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class MulitPointTouchListener implemen…
2011-09-29 22:15 · Android · 评论(0)
定时任务
定时任务有执行一次和重复执行两种 public class AlarmBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, R.string.show_message, Toast.LENGTH_SHORT).show(); } } /** * 执行一次 */ public void oneShotAlarm() { Intent intent = new Intent(this, Alarm…
2011-09-28 00:53 · Android · 评论(0)
几种创建LayoutInflater对象的方法
LayoutInflater layoutInflater; 1.layoutInflater = Context.getLayoutInflater(); 2.layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 3.layoutInflater = LayoutInflater.from(this);
2011-09-28 00:51 · Android · 评论(1)
判断网络类型及是否有网络
判断当前是否有可用网络 public boolean checkNetworkAvailable() { boolean isNetworkAvailable = false; android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE); if(connManager.getActiveNetworkInfo() != nu…
2011-09-28 00:49 · Android · 评论(0)
设置全屏
1.编程方式 public void setFullScreenMethod1(boolean isFullScreen) { if (isFullScreen) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); //也可以用下面的清除Flag //getWindow().clearFlags(WindowManager.LayoutPar…
2011-09-26 23:40 · Android · 评论(0)
dip与px单位之间转换
//转换dip为px public static int convertDIP2PX(Context context, int dip) { float scale = context.getResources().getDisplayMetrics().density; return (int)(dip*scale + 0.5f*(dip>=0?1:-1)); } //转换px为dip public static int convertPX2DIP(Context context, int px) { float scale = context.getResources().getDisp…
2011-09-25 10:11 · Android · 评论(0)
屏幕尺寸(高、宽)
Display display = getWindowManager().getDefaultDisplay(); display.getWidth();//返回px值 display.getHeight();//返回px值 display.getRotation();返回0或1,0表示竖屏,1表示横屏 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); dm.density; dm.heightPixels; dm.widthPixels; 看看上面…
2011-09-25 10:11 · Android · 评论(0)
判断手机外存(SD卡)状态
public int getExternalStorageState() { int result;//0:不可读写 1:只读 2:读写 String state = android.os.Environment.getExternalStorageState(); if(android.os.Environment.MEDIA_MOUNTED.equals(state)){ result = 2; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { result = 1; } else { result = 0; …
2011-09-25 10:07 · Android · 评论(0)
SQLite数据类型
一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存入值自动判断。SQLite具有以下五种数 据类型: 1.NULL:空值。 2.INTEGER:带符号的整型,具体取决有存入数字的范围大小。 3.REAL:浮点数字,存储为8-byte IEEE浮点数。 4.TEXT:字符串文本。 5.BLOB:二进制对象。 但实际上,sqlite3也接受如下的数据类型: smallint 16 位元的整数。 interger 32 位元的整数。 decimal(p,s) p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数点後有几位数。 如…
2011-09-25 10:03 · Android · 评论(0)
使用WebView实现腾讯微博OAuth登录
接着上一篇文章"使用WebView实现新浪微博OAuth登录",实现腾讯微博OAuth登录。 #腾讯官方下载SDK http://open.t.qq.com/resource.php?i=3,1 #申请应用KEY 登录腾讯微博,进入http://open.t.qq.com/申请应用,获取KEY和SECRET。 #准备 在项目中导入QWeiboSDK.jar、dom4j-1.6.1.jar(这两个包是由官方下载的SDK提供) 这里只给出腾讯OAuth登录代码,其它代码看上一篇文章 package com.oauth; import com.db.DbHelper; import com.ten…
2011-09-24 21:11 · Android · 评论(0)
使用WebView实现新浪微博OAuth登录
#新浪官方下载SDK(weibo4android) http://open.weibo.com/wiki/index.php/SDK #申请应用KEY 登录新浪微博,进入http://open.weibo.com/申请应用,获取KEY和SECRET。 #准备 在项目中导入commons-httpclient-3.x.jar并加入weibo4android、weibo4android.http、weibo4android.org.json、weibo4android.util包文件(这些都是官方下载的SDK提供的文件) 在跳转到新浪微博授权页面时会使用默认浏览器,这个不太好看,也有些用户手机用…
2011-09-20 12:18 · Android · 评论(4)