Android代码片段(持续更新)

调用系统邮件发送页面

 

String[] emailReceiver = {"xxxxx@yy.com"};
        
        Intent email = new Intent(Intent.ACTION_SEND);
//        email.setType("text/plain");
//        email.setType("plain/text");
        email.putExtra(Intent.EXTRA_EMAIL, emailReceiver);
        email.putExtra(Intent.EXTRA_SUBJECT, "邮件标题");
        email.putExtra(Intent.EXTRA_TEXT, "邮件内容啊, 有木有");
//        startActivity(email);
//        startActivity(Intent.createChooser(email, "请选择邮件发送软件"));
        
        
//        Uri uri = Uri.parse("/sdcard/1.txt");//提示文件太大,无法附加
        Uri uri = Uri.parse("file:///sdcard/1.txt");
        email.putExtra(Intent.EXTRA_STREAM, uri); 
//        email.setType("audio/mp3");
        //适用于任何类型的附件
        email.setType("message/rfc882");
        startActivity(email);

 

禁止横竖屏切换

传送门:http://guoyiqi.iteye.com/blog/1432166

 

//如:放到所有Activity基类中,即可禁止整个程序所有的重力感应
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

 上面设置对于有物理键盘的机型(如摩托里程碑),在物理键盘拉出或隐藏时,其系统内部横竖方向切换,Activity仍然会翻转,此时可设置竖屏显示:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

 

设置控件的坐标/大小等相关属性:

 

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this
.getLayoutParams();//RelativeLayout为当前控件所在布局文件的布局方式
params.topMargin = params.topMargin
+ this.mSpecialViewheight;//可设置坐标,宽高等属性(吐槽:获取属性一般都是getXXX(),get半天出不来...原来是直接public的)
this.setLayoutParams(params);

 

在OnCreate()方法中获取控件的高度和宽度,需要使用的一个观察者(否则得到的height, paddingTop等数据为0,因onCreate()执行时,控件还未加载完成) 

ViewTreeObserver vto = imageview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
// 此句若不执行,不影响控件宽高的获取结果,但是当前的onGlobalLayout()会执行多次,执行此句,当前方法只会执行一次
imageview.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
Log.i("TAG", imageview.getMeasuredHeight()
+ " -- - " + imageview.getHeight());
}
});

 

自定义Notification:

/**
*
* 方法名称:customNotification
* 作者:PiaoXingguo
* 方法描述:创建自定义格式的Notification(其实只需模仿notification.setLatestEventInfo(...)方法的源码即可)
* 输入参数:
@param context
* 返回类型:void
* 备注:
*/
private void customNotification(Context context) {
final NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.notification_icon, "custom", System.currentTimeMillis());
//R.layout.custom_notification 为自定义的xml布局文件,一个ImageView和两个TextView
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
contentView.setTextViewText(R.id.text, "custom text!");
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.title, "custom title!");
notification.contentView = contentView;

Intent intent = new Intent(context, TimeActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
notification.contentIntent = pi;
notification.flags = Notification.FLAG_AUTO_CANCEL;//设置标记

//1为id, 若要更新此Notification,需要用到
nManager.notify(1, notification);
}

 

判断当前应用是否是最上层的应用:

 

public static boolean isTopApp(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager
.getRunningTasks(5);
if (tasks == null || tasks.isEmpty()) {
return false;
}
RunningTaskInfo task = tasks.get(0);
String taskPackageName = task.topActivity.getPackageName();// task任务的package包名
System.out.println(taskPackageName);
// System.out.println(task.topActivity.getClassName());
// System.out.println(task.baseActivity.getClassName());
return context.getPackageName().equals(taskPackageName);
}

 

本机IP地址

Enumeration<NetworkInterface> networkInfo = NetworkInterface
.getNetworkInterfaces();
while (networkInfo.hasMoreElements())
{
NetworkInterface intf = networkInfo.nextElement();
Enumeration<InetAddress> intfAddress = intf
.getInetAddresses();
while (intfAddress.hasMoreElements())
{
InetAddress inetAddress = intfAddress.nextElement();
if (!inetAddress.isLoopbackAddress())
{
ip = inetAddress.getHostAddress().toString();
}
}
}

 

屏幕分辨率及其相关属性

DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println(dm.widthPixels + " - " + dm.heightPixels);

第二种方式获取DipalyMetrics对象(!= 上一种方式获取的对象 )

DisplayMetrics dm = activity.getResources().getDisplayMetrics();

更直接的:

//WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
宽度:this.getWindowManager().getDefaultDisplay().getWidth();


Activity页面切换效果:

new Handler().postDelayed(new Runnable() {
@Override
public void run()
{
Intent intent = new Intent(TransitionActivity.this, DeviceActivity.class);
startActivity(intent);
finish();
//页面切换 -- 淡入淡出效果
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
// overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
}
}, 5000);




posted @ 2012-03-13 00:08  当年明月  阅读(2006)  评论(0编辑  收藏  举报