Android开发常用经典代码段集锦

时间:2022-01-17 07:44:15

本文实例总结了android开发常用经典代码段。分享给大家供大家参考,具体如下:

1、图片旋转

?
1
2
3
4
5
6
bitmap bitmaporg = bitmapfactory.decoderesource(this.getcontext().getresources(), r.drawable.moon);
matrix matrix = new matrix();
matrix.postrotate(-90);//旋转的角度
bitmap resizedbitmap = bitmap.createbitmap(bitmaporg, 0, 0,
          bitmaporg.getwidth(), bitmaporg.getheight(), matrix, true);
bitmapdrawable bmd = new bitmapdrawable(resizedbitmap);

2、获取手机号码

?
1
2
3
4
5
6
7
8
9
10
//创建电话管理
telephonymanager tm = (telephonymanager)
//与手机建立连接
activity.getsystemservice(context.telephony_service);
//获取手机号码
string phoneid = tm.getline1number();
//记得在manifest file中添加
<uses-permission
android:name="android.permission.read_phone_state" />
//程序在模拟器上无法实现,必须连接手机

3.格式化string.xml 中的字符串

?
1
2
3
4
// in strings.xml..
<string name="my_text">thanks for visiting %s. you age is %d!</string>
// and in the java code:
string.format(getstring(r.string.my_text), "oschina", 33);

4、android设置全屏的方法

a.在java代码中设置

?
1
2
3
4
/** 全屏设置,隐藏窗口所有装饰 */
requestwindowfeature(window.feature_no_title);
getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
        windowmanager.layoutparams.flag_fullscreen);

b、在androidmanifest.xml中配置

?
1
2
3
4
5
6
7
<activity android:name=".login.netedit" android:label="@string/label_net_edit"
      android:screenorientation="portrait" android:theme="@android:style/theme.black.notitlebar.fullscreen">
 <intent-filter>
 <action android:name="android.intent.net_edit" />
 <category android:name="android.intent.category.default" />
 </intent-filter>
</activity>

5、设置activity为dialog的形式

在androidmanifest.xml中配置activity节点是配置theme如下:

?
1
android:theme="@android:style/theme.dialog"

6、检查当前网络是否连上

?
1
2
3
connectivitymanager con=(connectivitymanager)getsystemservice(activity.connectivity_service);
boolean wifi=con.getnetworkinfo(connectivitymanager.type_wifi).isconnectedorconnecting();
boolean internet=con.getnetworkinfo(connectivitymanager.type_mobile).isconnectedorconnecting();

在androidmanifest.xml 增加权限:

?
1
<uses-permission android:name="android.permission.access_network_state" />

7、检测某个intent是否有效

?
1
2
3
4
5
6
7
8
public static boolean isintentavailable(context context, string action) {
  final packagemanager packagemanager = context.getpackagemanager();
  final intent intent = new intent(action);
  list<resolveinfo> list =
      packagemanager.queryintentactivities(intent,
          packagemanager.match_default_only);
  return list.size() > 0;
}

8、android 拨打电话

?
1
2
3
4
5
6
7
try {
  intent intent = new intent(intent.action_call);
  intent.setdata(uri.parse("tel:+110"));
  startactivity(intent);
} catch (exception e) {
  log.e("sampleapp", "failed to invoke call", e);
}

9、android中发送email

?
1
2
3
4
5
6
7
intent i = new intent(intent.action_send);
//i.settype("text/plain"); //模拟器请使用这行
i.settype("message/rfc822") ; // 真机上使用这行
i.putextra(intent.extra_email, new string[]{"test@gmail.com","test@163.com});
i.putextra(intent.extra_subject,"subject goes here");
i.putextra(intent.extra_text,"body goes here");
startactivity(intent.createchooser(i, "select email application."));

10、android中打开浏览器

?
1
2
3
intent viewintent = new
  intent("android.intent.action.view",uri.parse("http://vaiyanzi.cnblogs.com"));
startactivity(viewintent);

11、android 获取设备唯一标识码

?
1
string android_id = secure.getstring(getcontext().getcontentresolver(), secure.android_id);

12、android中获取ip地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public string getlocalipaddress() {
  try {
    for (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces();
 en.hasmoreelements();) {
      networkinterface intf = en.nextelement();
      for (enumeration<inetaddress> enumipaddr = intf.getinetaddresses();
 enumipaddr.hasmoreelements();) {
        inetaddress inetaddress = enumipaddr.nextelement();
        if (!inetaddress.isloopbackaddress()) {
          return inetaddress.gethostaddress().tostring();
        }
      }
    }
  } catch (socketexception ex) {
    log.e(log_tag, ex.tostring());
  }
  return null;
}

13、android获取存储卡路径以及使用情况

?
1
2
3
4
5
6
7
8
9
10
/** 获取存储卡路径 */
file sdcarddir=environment.getexternalstoragedirectory();
/** statfs 看文件系统空间使用情况 */
statfs statfs=new statfs(sdcarddir.getpath());
/** block 的 size*/
long blocksize=statfs.getblocksize();
/** 总 block 数量 */
long totalblocks=statfs.getblockcount();
/** 已使用的 block 数量 */
long availableblocks=statfs.getavailableblocks();

14 android中添加新的联系人

?
1
2
3
4
5
6
7
8
9
10
11
private uri insertcontact(context context, string name, string phone) {
    contentvalues values = new contentvalues();
    values.put(people.name, name);
    uri uri = getcontentresolver().insert(people.content_uri, values);
    uri numberuri = uri.withappendedpath(uri, people.phones.content_directory);
    values.clear();
    values.put(contacts.phones.type, people.phones.type_mobile);
    values.put(people.number, phone);
    getcontentresolver().insert(numberuri, values);
    return uri;
}

15、查看电池使用情况

?
1
2
intent intentbatteryusage = new intent(intent.action_power_usage_summary);
startactivity(intentbatteryusage);

16、获取进程号

?
1
2
3
4
5
6
activitymanager mactivitymanager = (activitymanager) this.getsystemservice(activity_service);
list<activitymanager.runningappprocessinfo> mrunningprocess = mactivitymanager.getrunningappprocesses();
int i = 1;
for (activitymanager.runningappprocessinfo amprocess : mrunningprocess) {
 log.e("homer application", (i++) + " pid = " + amprocess.pid + "; processname = " + amprocess.processname);
}

希望本文所述对大家android程序设计有所帮助。