Android 開發相關注意點

來源:互聯網
上載者:User

關於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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.