首页 > 其他分享 >Android 开发相关注意点

Android 开发相关注意点

时间:2023-01-28 18:31:08浏览次数:39  
标签:tm string int public 开发 注意 new Android android


关于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>








标签:tm,string,int,public,开发,注意,new,Android,android
From: https://blog.51cto.com/tody/6025090

相关文章

  • ethcat开发记录 一
    一、方案1、移植开源方案SOEM2、专用芯片二、SOEM移植(一)硬件stm32f407,168MPHY:LAN8720A (ii)Pointstonote1,thePHYaddressoftheLAN8720problem.2,......
  • ethcat开发记录 二
    SOEM移植到stm32f407+LAN8720硬件上的注意点1、LAN8720的PHY地址问题。2、LAN8720芯片在上电后要对复位引脚操作。3、使能LAN8720的混杂模式,在新的HAL库里,这个设置包含......
  • 1.1 移动开发技术简介
    本节将主要介绍一下移动开发技术的进化历程,了解一下Flutter技术出现的背景。笔者认为,了解一门新技术出现的背景是非常重要的,因为只有了解之前是什么样的,才能理解为什么会......
  • Huaweicloud for 开发语言
    连续参加过几次华为云第三方满意度调研,我对华为云对开发者最不满意的一点就是对Top10常用语言的支持上很差,特别是对.NET的支持。经过连续多年(至少有3年了)的不断反馈,今天很......
  • WPF开发经验-实现一种机械泵控件
    一引入考虑实现一种机械泵控件。机械泵是工业中通常用来制造真空的一类设备,我们在绘制界面UI时希望可以生动形象地来表述一个机械泵,下面讲述了一种简单的实现。二 Me......
  • ISM Web组态软件的组态应用开发之组态界面简介
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录​​前言​​​​一、组件样式​​​​二、组件的事件​​​​三、组件的动作​​​​四、组件的动画......
  • 第二节 使用ISM Web组态软件利用采集到的Modbus设备数据开发组态应用
    系列文章目录​​第一节使用ISMWeb组态软件采集Modbus设备数据​​第二节使用ISMWeb组态软件利用采集到的Modbus设备数据开发组态应用文章目录​​系列文章目录​​​​......
  • macos:安装java 17.0.6(android studio报错:Unable to locate a Java Runtime.)
    一,报错信息:androidstudio的报错信息:Theoperationcouldn’tbecompleted.UnabletolocateaJavaRuntime.Pleasevisithttp://www.java.comforinformation......
  • 【网关开发】6.lua绑定委托(delegate)实现多播调用
    目录背景实现原理实现细节应用测试思考与总结背景在程序开发过程中有时会遇到事件流的问题,某一个结果会触发A、B、C等一系列动作。需要将各种事件注册给委托类(Delegate)。......
  • 测试开发之流量录制回放,压测有道
    1、JVM-sandbox-repeater介绍2、可以应用到哪些场景?3、安装部署《本文由测试开发攻城狮王月明投稿》一、JVM-sandbox-repeater介绍JVM-sandbox(沙箱)是阿里提供的开源......