安卓的系统架构
- Linux操作系统及驱动
- 本地框架及java运行环境
- java框架
- java应用程序
Intent
使用intent显示启动Activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
//方法二
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
使用intent隐式启动Activity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
startActivity(intent);
使用intent在Activity之间传递数据
直接使用Intent在Activity之间传递数据
btn.setOnclickListener(new View.OnClickListener(){
public void click(){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name", "tom");
intent.putExtra("pass", 12121);
startActivity(intent);
}
})
//接受数据
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
Int pass = intent.getIntExtra("pass", 0);
使用Bundle在Activity之间传递
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putInt("pass", 5);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(bundle);
startActivity(intent);
//接受数据
Intent intent = this.getIntent();
Bundle bundle = intent.getExtra();
String name = bundle.getString("name");
Int pass = bundle.getInt("pass");
Activity的生命周期
- onCreate()---------Activity启动后第一个被调用的函数,常用来进行Activity的初始化
- onStart()-----------当Activity显示在屏幕上,该函数被调用
- onRestart()-----------当Activity从停止状态进入活动状态,调用该函数
- onResume()-----------当Activity能够与用户进行交互时,该函数被调用
- onPause()-----------当Activity进入暂停状态时,该函数被调用
- onStop()----------当Activity进入停止状态时,该函数被调用
- onDestory()--------当Activity被中止时,该函数被调用
Spinner的事件监听机制
OnItemSelectedListener接口
public void onItemSelected(AdapterView<?>parent, View view, int position, Long id)
AdapterView:选项被选中的视图列表
view:当前选中的item选项
position: 被选中的item选项在视图的位置
id: 被选中item选项的id
ListView
数据源:
private String[] names = new String[]{"虎头", "虎脑"};
private String[] imageids = new int[]{R.drawable.tiger, R.drawable.tiger_head};
public class MainActivity extends Activity{
super.onCreate(saveInstanceState);
setContentView()R.layout.main);
private list<Map<String,Object>> list = new ArrayList<>;
for (int i = 0; i < names.lenght; i++){
Map<String,Object> map = new HashMap<>;
map.put("name", names[i]);
map.put("image", imageids[i]);
list.add(map);
}
//建立适配器
SimpleAdapter simpleadapter = new SimpleAdapter(this, list, R.layout.simple_item, new String[]{"name", "image"}, new int[]{R.id.name, R.id.image});
ListView listview = findViewById(R.id.listview);
//绑定
list.setAdapter(simpleadapter);
}
Fragment的动态创建
<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:id="@+id/container"
android:orientation="horizontal"
/>
//1.根据上下文获取FragmentManager对象
FragmentManager manager = this.getFragmentManager();
//2.使用获取到的manager对象开启一个事务
FragmentTransaction mFragmentTransaction01 = manager.beginTransaction();
//3.替换Fragment
mFragmentTransaction01.replace(R.id.container, new AddFragmentFirst());
//4.提交事务
mFragmentTransaction01.commit();
SharedPreferences
写入数据
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
preferences = getSharedPreferences("shared", Content.MODE_PRIVATE);
editor = perferences.edit();
editor.putString("name", "jkj");
editor.putInt("pass", 555);
editor.apply();
读取数据
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
perferences = getSharedPreferences("shared". Content.MODE_PRIVATE);
String name = perferences.getString("name");
Int pass = perferences.getInt("pass");
SQLiteDatabase
创建SQLiteOpenHelper
public class SqlUtils extends SQLiteOpenHelper{
public SqlUtils(Context context, String name, SQLiteDatabase CursorFactory factory, int version){
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("create table teacher(id integer, name varchar(20));
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}
}
创建sql_methods
public class sql_methods{
public static void add(Context context, Teacher teacher){
SqlUtils sqlUtils = new SqlUtils(context, "tescherdb", null, 1);
SQLiteDatabase db = sqlUtils.getReadableDatabase();
String swl = "insert into teacher(id, name) values(?, ?);
db.execSQL(sql, new Boject[]{teacher.getId(), teacher.getName()};
db.close();
}
//修改
update teacher set name = ? where id = ?
//删除
delete from teacher where id = ?
String sql = "select * from teacher where teacher_id like ? and teacher_name like ? and teacher_gender like ? ";
Cursor cursor = db.rawQuery(sql,new String[]{"%"+teacher_id+"%","%"+teacher_name+"%","%"+teacher_gender+"%"});
while(cursor.moveToNext()){
Map<String,Object> map = new HashMap<>();
map.put("map_id",cursor.getInt(0));
map.put("map_name",cursor.getString(1));
map.put("map_gender",cursor.getString(2));
map.put("map_salary",cursor.getInt(3));
list.add(map);
}
if(list.isEmpty()){
Toast.makeText(context,"没有查询结果",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"查询了"+list.size()+"条结果",Toast.LENGTH_SHORT).show();
}
cursor.close();
db.close();
return list;
}
Service组件
public class BindClass extends Service{
private MyBinder binder = new MyBinder();
class MyBinder extends Binder{
public int getcount(int n){
return BindClass.this.sum(n);
}
}
public IBinder onBinde(Intent intent){
return binder;
}
private int sum(int n){
itn sum = 0;
for (int i = 0; i <= n; i++){
sum += i;
}
return sum;
}
}
public class MainActivity extends AppCompatActivity{
private BindClass.MyBinder binder;
private ServiceConnection conn = new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder service){
binder = (BindeClass.MyBidner)service;
}
public void onserviceDisconnected(Component name){}
}
public void onCreate(Bundle savedInstence){
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, BindClass.class);
//绑定
bindService(intent, conn, Service.BIND_AUTO_CREATE);
//解除绑定
unbindService(coon);
//调用service方法
binder.getcount(10);
}
}
boradcast
发送广播
Intent intent = new Intent();
intent.setAction("com.example.boradcast.COM_BORADCAST");
intent.setPackge("com.example.boradcast");
intent.putExtra("msg", "12121“);
sendBroadcast(intent);//普通广播
sendOrderedBroadcast(intent, null);//有序广播
public class MyReciver_0 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ACTION" + intent.getAction() + "msg" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("first", "第一个的消息");
setResultExtras(bundle);
}
}
public class Myreciver_1 extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
String first = bundle.getString("first");
Toast.makeText(context, "first" + first, Toast.LENGTH_SHORT).show();
}
}
<receiver android:name=".Myreciver_1">
<intent-filter android:priority="10">
<action android:name="com.example.boradcast.COM_BORADCAST"></action>
</intent-filter>
</receiver>