Android 开发相关注意点

时间:2023-01-28 18:59:21


关于Intent实例调用:

1,调用外部程序:

   参数1:Process Name

   参数2:Application Class

try{
Intent eth = new Intent();
eth.setClassName("com.android.settings", "com.android.settings.EthernetSettings");
startActivity(eth);
}catch(ActivityNotFoundException e)
{
//deal error code
return ;
}

2,调用SDK自带的

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

3,调用自己写的实例

startActivity(new Intent(this, timecheck.class));

关于添加单击事件处理代码:

声明时需要调用接口:public class ITester extends Activity implements OnClickListener{...}

并添加单击事件:     public void onClick(View v)

View dateTimeBtn = findViewById(R.id.btn_time_chk); dateTimeBtn.setOnClickListener(this);

如果需要获得单击的具体对象,需要用到方法:   v.getId();

例如:

public void onClick(View v)
{
switch(v.getId())
{
case R.id.btn_time_chk:
startActivity(new Intent(this, timecheck.class));
break;
}
}

其他代码实例:

1,或者内部eMMC容量大小

File path = Environment.getDataDirectory();   
StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long aBlocks = stat.getAvailableBlocks();

float Tsize = (float)blockSize * totalBlocks/(1024*1024*1024);
float Asize = (float)blockSize * aBlocks/(1024*1024*1024);


2,获取系统当前时间:

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.widget.TextView;


public class timecheck extends ITester{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timecheck);

handler.postDelayed(runnable, 1000); //delay 1000ms=1s

}

Handler handler=new Handler();
Runnable runnable=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
TextView timeV = (TextView)findViewById(R.id.timeView);
Time tm = new Time();

tm.setToNow(); //Get local time

int year = tm.year;
int mon = tm.month + 1;
int day = tm.monthDay;
int hour = tm.hour;
int min = tm.minute;
int sec = tm.second;

timeV.setTextSize(39);
timeV.setTextColor(Color.BLUE);
timeV.setText("Now Time is: \n "+ year + "-" + mon + "-"
+ day + " " + hour + ":" + min + ":" + sec);
handler.postDelayed(this, 500); //delay 500ms=0.5s
}
};
}

3,文件读写实例:

File f = new File("/sdcard/sdt.txt");

f.createNewFile();
FileOutputStream wf = new FileOutputStream(f);
wf.write("\nSD_Test_String_from_iTester\nWritten By Tody \n(c) 2011 T-ware Inc.\n".getBytes()); // write string to file...
wf.flush();
wf.close();File f = new File("/proc/meminfo");
BufferedReader r = new BufferedReader(new InputStreamReader( new FileInputStream( f )),32);
String line = r.readLine();

4,对话框弹出实例:                new AlertDialog.Builder(this).setMessage("请按电源按钮选择休眠(S3)测试").show();

5,振动器实例:

private Vibrator iVibrator;
iVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
iVibrator.vibrate(100);
vibrateBtn.setBackgroundColor(Color.GREEN);


6,取消对话框实例:

new AlertDialog.Builder (this)
.setTitle ("Alerting Message")
.setMessage ("eek!")
.setNegativeButton ("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do nothing - it will close on its own
}
})
.show ();

7,确定对话框实例:

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
builder.setIcon(R.drawable.icon);
builder.setTitle("你确定要离开吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
showDialog("你选择了确定");
}
});




AndroidManifest.xml:定义权限及程序配置等:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.itester"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".ITester"
android:label="@string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.catetory.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".about"
android:label="@string/about_title">
</activity>

<activity android:name=".timecheck"
android:label="@string/now_time_title"
android:theme="@android:style/Theme.Dialog">
</activity>

<activity android:name=".sdcard"
android:label="@string/sd_title"
android:theme="@android:style/Theme.Dialog">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</activity>

<activity android:name=".meminfo"
android:label="@string/mem_info_title"
android:theme="@android:style/Theme.Dialog">
</activity>

<activity android:name=".rgbchk"
android:label="@string/rgb_chk">
</activity>

</application>
</manifest>