android学习记录
拨打电话功能:
添加权限 <uses-permission android:name="android.permission.CALL_PHONE"/>
主要方法 Intent intent = new Intent(content,Uri.parse("tel:"+phoneNo));
短信发送功能
添加权限<uses-permission android:name="android.permission.SEND_SMS"/>
主要方法
SmsManager smsManger = SmsManager.getDefault();
if(smsContext.length()>70){
ArrayList<String> count = smsManger.divideMessage(smsContext);
for(String sms:count){
smsManger.sendTextMessage(phoneNo, null, sms, null, null);
}
}else{
smsManger.sendTextMessage(phoneNo, null, smsContext, null, null);
}
TextView跳转Activity
String str = "跳转";
SpannableString ss = new SpannableString(str);
ss.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,SMSActivity.class);
startActivity(intent);
}
}, 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
toSMS.setText(ss);
toSMS.setMovementMethod(LinkMovementMethod.getInstance());
(toSMS)为TextView
布局
RelativeLayout属性
align 对齐,margin 距离,padding 外边距,gravity 内容
第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
配置单元测试
在AndroidManifest.xml中
添加1:
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="###"/>
###代表
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phone"
android:versionCode="1"
android:versionName="1.0" > 中 package的值。
添加2:在application标签内添加<uses-library android:name="android.test.runner"/>
单元测试类需继承AndroidTestCase类。写好测试方法后,点击eclipse大纲视图中写完的测试方法 run as---》android junit
断言 assertEquals(78, a); 表示:期望值是78,a为变量,代表实际运算值。
日志
Log.i i代表info
Log.d d代表debug
Log.e e代表error
Log.w w代表warn
文件读取 (context 为Activity中传入的Content对象)
public void writeFile(String fileName,String fileContent) throws IOException{
//通过上下文对象context获取文件输出流 ,MODE_PRIVATE表示私有模式,当该应用创建过该名称的文件,文件将被覆盖
//Context.MODE_APPEND表示追加模式,如果文件存在,将内容追加到文件末尾
//Context.MODE_WORLD_READABLE 允许其他应用读取内容
//Context.MODE_WORLD_WRITEABLE 允许其他应用修改内容
//Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE为可读写
FileOutputStream outStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
outStream.write(fileContent.getBytes());
outStream.close();
}
public String readFile(String fileName) throws IOException{
FileInputStream inStream = context.openFileInput(fileName);
byte[] buffer = new byte[1024];
int len = 0;
//存入内存中
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
while((len=inStream.read(buffer))!=-1){
byteOutStream.write(buffer, 0, len);
}
byte[] b = byteOutStream.toByteArray();
return new String(b);
}
创建的文件在data\data\包名\file 下找到,如果是真机,需root,也是在该路径找到文件。
写入SD卡 (context 为Activity中传入的Content对象)
//判断手机上是否有sd卡,并且可读写
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
saveToSDcard(fileName,fileContent);
}
public void saveToSDcard(String fileName,String fileContent) throws IOException{
//Environment.getExternalStorageDirectory()得到SD卡路径
File file = new File(Environment.getExternalStorageDirectory(),fileName);
OutputStream outStream = new FileOutputStream(file);
outStream.write(fileContent.getBytes());
outStream.close();
}
SharePreference
把参数存储在 SharePreference,最终保存在xml中。getSharedPreferences()中第二个参数,与 上面的文件读取的参数相同
也可以用 SharedPreferences sharedPreferences= getPreferences(Context.MODE_PRIVATE); 默认文件名,只需设置权限模式
//存入
SharedPreferences sharedPreferences = getSharedPreferences("fileName", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("name", name.getText().toString());
editor.putInt("age", new Integer(age.getText().toString()));
editor.commit();
//取出 getSharedPreferences第二个参数随意
SharedPreferences sharedPreferences = getSharedPreferences("fileName", Context.MODE_PRIVATE);
//第二个参数代表:如果没有叫name的字符串时,表示默认值
String pname =sharedPreferences.getString("name", "");
int page =sharedPreferences.getInt("age", 0);
name.setText(pname);
age.setText(String.valueOf(page));
访问其他应用的SharePreference
//Context.CONTEXT_IGNORE_SECURITY 忽略安全监测
Context context =getContext().createPackageContext("包名", Context.CONTEXT_IGNORE_SECURITY);
context.getSharedPreferences("fileName", context.MODE_WORLD_READABLE);
sqlite
类继承SqliteOpenHelper类。
增删改的套路
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from person where pid=?",new Object[]{id.toString()});
查询
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(" select * from person where pid=?", new String[]{id.toString()});
if(cursor.moveToFirst()){
int pid = cursor.getInt(cursor.getColumnIndex("pid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
return new Person(pid, name);
}
如果数据库空间未满的情况下,getReadableDatabase()方法是可以进行写操作的,见源码。
Notification
private NotificationManager nm;
private Button bt1,bt2;
private static final int NOTIFICATION_ID = 0x123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
send(v);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nm.cancel(NOTIFICATION_ID);
}
});
}
@SuppressLint("NewApi")
public void send(View view){
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
//PendingIntent是对intent的封装,不会立即触发跳转
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification notification = new Notification.Builder(this).setTicker("新消息")
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("一条新消息")
.setContentText("好消息,好消息,全场五折")
.setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
.setContentIntent(pi).build();
nm.notify(NOTIFICATION_ID, notification);
}
//提示灯和振动权限
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>