您的位置:首页 > 移动开发 > Android开发

Android中的一些常用功能的关键代码

2017-08-21 14:24 399 查看


功能(有些需要权限,自行添加)


1.拨号

第一种方法



 

第二种方法 




2.SeekBar进度条




3.沉浸式状态栏

放在onCreate()方法里





4.隐藏ActionBar

getActionBar().hide();
1
1


5.屏蔽返回键




6.按两次退出

方法1



 

方法2
private long exitTime = 0;
1
1




7.拍照




8.分享

调用系统的分享功能





9.播放音乐

对应的功能,比如停止时stop();





10.ViewPager




11.获取软件版本号




12.检查SD卡状态




13.手机震动




14.跳转短信




15.头像选择




16.获取系统电量




17.下载




18.重启

root





19.关机

root





20.子线程网络下载图片




21.获取手机各种信息

效果图



 

直接上代码 

注意添加各种权限


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/tab1_rl"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#34c083" >

<TextView
android:id="@+id/tab1_btn01"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_centerHorizontal="true"
android:background="@null"
android:gravity="center"
android:text="手机信息"
android:textColor="@android:color/white"
android:textSize="20dp" />

<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_alignParentLeft="true"
android:background="@drawable/back" />
</RelativeLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="IP:" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="MAC地址:" />

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="SD卡存储信息:" />

<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="手机分辨率:" />

<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="网络是否连接:" />

<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="手机内存:" />

<TextView
android:id="@+id/tv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="手机CPU:" />

<TextView
android:id="@+id/tv7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="手机IMEI:" />

<TextView
android:id="@+id/tv8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />

</LinearLayout>
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233


MainActivity.java

package com.lgl.circle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.lgl.circle.R;

public class AboutPhone extends Activity {

private TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8;
private String[] cpuInfo = { "", "" }; // 1-cpu型号 //2-cpu频率

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about_phone);
initView();
}

private void initView() {
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText(getLocalIpAddress2());
tv2 = (TextView) findViewById(R.id.tv2);
tv2.setText(getLocalMacAddress());
tv3 = (TextView) findViewById(R.id.tv3);
// 需要判断手机上面SD卡是否插好,如果有SD卡的情况下,我们才可以访问得到并获取到它的相关信息,当然以下这个语句需要用if做判断
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 取得sdcard文件路径
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
// 获取block的SIZE
long blocSize = statfs.getBlockSize();
// 获取BLOCK数量
long totalBlocks = statfs.getBlockCount();
// 空闲的Block的数量
long availaBlock = statfs.getAvailableBlocks();
// 计算总空间大小和空闲的空间大小
// 存储空间大小跟空闲的存储空间大小就被计算出来了。
long availableSize = blocSize * availaBlock;
// (availableBlocks * blockSize)/1024 KIB 单位
// (availableBlocks * blockSize)/1024 /1024 MIB单位
long allSize = blocSize * totalBlocks;
tv3.setText("可用:" + availableSize / 1024 / 1024 / 1024 + "GB"
+ "  总共:" + allSize / 1024 / 1024 / 1024 + "GB");
} else {
tv3.setText("SD卡不可用");
}

tv4 = (TextView) findViewById(R.id.tv4);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
tv4.setText("分辨率:" + displayMetrics.widthPixels + "x"
+ displayMetrics.heightPixels);

tv5 = (TextView) findViewById(R.id.tv5);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
// info.setConnected(false);
tv5.setText("当前无网络");
} else {
// info.setConnected(true);
tv5.setText("当前网络正常");
}
tv6 = (TextView) findViewById(R.id.tv6);
String availMemory = getAvailMemory();
String totalMemory = getTotalMemory();
tv6.setText("可用内存:" + availMemory + "\n" + "总内存:" + totalMemory);
tv7 = (TextView) findViewById(R.id.tv7);
getCpuInfo();
tv7.setText("cpu型号:" + cpuInfo[0] + "\n" + "cpu频率:" + cpuInfo[1]);
tv8 = (TextView) findViewById(R.id.tv8);
try {
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTelephonyMgr.getDeviceId();
tv8.setText("" + imei);

} catch (Exception e) {
// TODO: handle exception
}
findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
finish();
}
});
}

// 获取手机ip method-2
public String getLocalIpAddress2() {
// 获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// 判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
return ip;
}

private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}

/**
* mac地址 start
*/
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();

return info.getMacAddress();
}

// 手机的内存信息主要在/proc/meminfo文件中,其中第一行是总内存,而剩余内存可通过ActivityManager.MemoryInfo得到。

private String getAvailMemory() {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// mi.availMem; 当前系统的可用内存
return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
}

private String getTotalMemory() {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小

arrayOfString = str2.split("\\s+");
for (String num : arrayOfString) {
Log.i(str2, num + "\t");
}

initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();

} catch (IOException e) {
}
return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化
}

/**
* CPU
*/
private void getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: