Android学习第一天笔记

时间:2022-03-05 05:38:55

Android基础第一天

adb

什么是adb

android debug bridge

将adb添加到环境变量里面去

adb在platform-tool目录下面

adb命令

  • adb start-server,启动adb
  • adb kill-server,关闭adb
  • adb install apk名字,安装apk到android设备里面
  • adb uninstall 应用包名,卸载某个app
  • adb devices,列出与开发环境建立连接的设备列表
  • adb shell,进入android设备命令行
  • netstae -ano,windows上查看进程是否占用了adb的端口,占用了就把那个进程杀死

打电话demo

  • MainActivity.java
package com.dasudian.callphone;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button call = (Button) findViewById(R.id.bt_call);
call.setOnClickListener(new MyClickListener());
}

class MyClickListener implements View.OnClickListener {

@Override
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.et_phone);
String phone = et.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phone));
startActivity(intent);
}
}
}
  • manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dasudian.callphone">

<!-- 添加打电话权限,不然运行时会崩溃 -->
<uses-permission android:name="android.permission.CALL_PHONE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dasudian.callphone.MainActivity">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入电话号码" />


<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


<Button
android:id="@+id/bt_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨号" />

</LinearLayout>

发短信demo

  • MainActivity.java文件
package com.dasudian.smssend;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText et_phone = (EditText) findViewById(R.id.et_phone);
final EditText et_content = (EditText) findViewById(R.id.et_content);
Button send = (Button) findViewById(R.id.bt_send);
assert send != null;
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = et_phone.getText().toString();
String content = et_content.getText().toString();

SmsManager smsManager = SmsManager.getDefault();
// 当内容太长的时候,分割后在发送,不然由于内容太长无法发送出去。
ArrayList<String> smss = smsManager.divideMessage(content);
for (String string : smss) {
smsManager.sendTextMessage(phone, null, string, null, null);
}
}
});
}
}
  • manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dasudian.smssend">

<!--需要添加发送短信的权限,不然程序运行会崩溃 -->
<uses-permission android:name="android.permission.SEND_SMS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dasudian.smssend.MainActivity">


<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码"
android:inputType="phone" />


<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="请输入短信内容"
android:lines="5" />


<Button
android:id="@+id/bt_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />

</LinearLayout>