一、Activity跳转
1.创建第二个Activity
创建activity2类
package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
创建activity_2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
配置文件(AndroidManifest.xml)中写入
<activity android:name=".Activity2"/>
跳转按键 在activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转"
android:onClick="startActivity2"/>
main文件中创建跳转方法
public void startActivity2(View view) {
startActivity(new Intent(this,Activity2.class));
}
二、Activity的生命周期
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: ");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
三、Service
这个相当于一个后台的服务,创建后,需要销毁服务才会结束
有两种模式1.startService 2.bindService
创建一个MyService
MyService类
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
}
在AndroidManifest.xml下注册
<service android:name=".MyService"/>
MainActivity类
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
public void startServce(View view) {
startService(new Intent(this, MyService.class));
}
public void stopServce(View view) {
stopService(new Intent(this, MyService.class));
}
public void bindServce(View view) {
bindService(new Intent(this, MyService.class), connecton, Context.BIND_AUTO_CREATE);
}
public void unBindServce(View view) {
unbindService(connecton);
}
private ServiceConnection connecton = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connecton);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity3"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService 对应 stopService 区域"
android:textSize="20dp"
android:textColor="@color/design_default_color_secondary"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startServce"
android:onClick="startServce"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopServce"
android:onClick="stopServce"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService 对应 unBindService 区域"
android:textSize="20dp"
android:textColor="@color/black"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startServce"
android:onClick="bindServce"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startServce"
android:onClick="unBindServce"/>
</LinearLayout>
</LinearLayout>
四、Receiver
广播:分为系统广播和用户自定义广播
蓝牙断开,WiFi断开,电源没电
一、静态注册接受广播
MainActivity ----> 广播接收者
注册文件中
<receiver
android:name=".CustomReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.derry.receiver_flag_" />
</intent-filter>
</receiver>
创建一个接口类
package com.example.myapplication;
public interface ActionUtils {
String ACTION_EQUES_UPDATE_IP = "com.derry.receuver_study_";
String ACTION_FLAG = "com.derry.receiver_flag_";
}
创建MainActivity4
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
}
public void sendAction1(View view) {
}
public void sendAction2(View view) {
Intent intent = new Intent();
intent.setAction(ActionUtils.ACTION_FLAG);
sendBroadcast(intent);
}
}
mainActivity的页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity4"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态注册广播 发送广播 区域"
android:textSize="20dp"
android:textColor="@color/design_default_color_secondary"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="sendAction1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="静态注册广播 发送广播 区域"
android:textSize="20dp"
android:textColor="@color/black"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="sendAction2"/>
</LinearLayout>
二、动态注册接收广播
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
public class MainActivity4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
UpdataIpSelectCity updataIpSelectCity = new UpdataIpSelectCity();
IntentFilter filter = new IntentFilter();
filter.addAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
registerReceiver(updataIpSelectCity,filter);
}
public void sendAction1(View view) {
Intent intent = new Intent();
intent.setAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
sendBroadcast(intent);
}
public void sendAction2(View view) {
Intent intent = new Intent();
intent.setAction(ActionUtils.ACTION_FLAG);
sendBroadcast(intent);
}
}
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//第一步,定义好广播接收者
public class UpdataIpSelectCity extends BroadcastReceiver {
private static final String TAG = UpdataIpSelectCity.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "UpdataIpSelectCity onReceive 广播接受者");
}
}