赞
踩
Android系统架构
https://blog.csdn.net/xzzteach/article/details/140904613
Android四大组件
Activity
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
函数 | 描述 |
---|---|
onCreat() | 一个Activity启动后第一个被调用的函数,常用来在此方法中进行Activity的一些初始化操作。例如创建View,绑定数据,注册监听,加载参数等。 |
onStart() | 当Activity显示在屏幕上时,此方法被调用但此时还无法进行与用户的交互操作。 |
onResume() | 英文词义简历、继续进行 这个方法在onStart()之后调用,也就是在Activity准备好与用户进行交互的时候调用,此时的Activity一定位于Activity栈顶,处于运行状态。 |
onPause() | 这个方法是在系统准备去启动或者恢复另外一个Activity的时候调用,通常在这个方法中执行一些释放资源的方法,以及保存一些关键数据。 |
onStop() | 这个方法是在Activity完全不可见的时候调用的。 |
onDestroy() | 这个方法在Activity销毁之前调用,之后Activity的状态为销毁状态。 |
onRestart() | 当Activity从停止stop状态恢进入start状态时调用状态。 |
实际操作过程中,当打开启动Android应用时,MainActivity
会经过onCreate
-> onStart
-> onResume
三个阶段,此时若按home键或则back按键,MainActivity会经过onPause -> onStop
这两个阶段,再进入此MainActivity时,会再调用onRestart -> onStart -> onResume
三个阶段。
3、Activity 启动模式
启动模式一共有 4 中:standard
、singleTop
、singTask
和 singleInstance
,可以在 AndroidManifest.xml 中通过 标签指定 android:launchMode 属性来选择启动模式。
standard模式
standard 模式是 Activity 的默认启动模式,在不进行显示指定的情况下,所有 Activity 都会自动使用这种启动模式。对于使用 standard 模式启动的 Activity,系统不会在乎这个 Activity 是否已经存在在栈中了。每次启动的时候都会创建一个新的实例。一般用于打开邮件之类的。
singleTop模式 (栈顶复用模式)
如果 Activity 指定为 singleTop,在启动 Activity 的时候发现返回栈的栈顶已经是该 Activity 了。则认为可以直接使用它,就不会再创建新的 Activity 实例了。因为不会创建新的 Activity 实例,所以 Activity 的生命周期就没有什么变化了。假设你在当前的Activity中又要启动同类型的Activity,此时建议将此类型Activity的启动模式指定为SingleTop,能够降低Activity的创建,节省内存!
一般用于登录页面,新闻详情页等。
singTask 模式(栈内单例模式)
singleTop 很好的解决了重复创建栈顶 Activity 的问题。如果 Activity 没有处于栈顶的位置,还是可能会创建多个 Activity 实例的。那就需要借助 singleTask 了。当 Activity 的启动模式为 singleTask 的时候,每次启动该 Activity 的时候系统会首先在返回栈中检查是否存在该 Activity 的实例,如果发现已经存在则直接使用该实例。并把这个 Activity 之上的所有 Activity 全部出栈,如果没有就会创建一个新的 Activity 实例。
一般主页面使用,购物页面,付款页面,浏览器主页等
singleInstance模式(堆内单例模式)
singleInstance 模式的 Activity 会启用一个新的返回栈来管理这个 Activity 。在这种模式下会有一个单独的返回栈来管理这个 Activity,不管是哪个应用程序来访问这个 Activity,都共用的同一个返回栈,也就解决了共享 Activity 实例的问题。
4、Activity 之间相互跳转及通信
Intent(意图)是应用程序种各个组件联系的桥梁,通信的载体,负责应用程序中数据的传递。
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
此段代码实现从当前Activity,跳转到MainActivity,不带任何参数。
String data = "是我主活动调用了你";
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("ext", data);
startActivity(intent);
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
Bundle bundle = new Bundle() ;
bundle.putString("register","请开始注册!");
intent.putExtras(bundle) ;
startActivityForResult(intent,1);
在跳转时使用startActivityForResult方法,此方法传入两个参数一个是Intent ,另外一个是给当前请求设置的一个请求ID,此ID在结束数据时辨识是否使当前请求的返回结果。
同时要在跳转源Activity中实现 onActivityResult 方法来接收处理目的Activity返回的数据。
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode == RESULT_OK) { final EditText loginUsername = findViewById(R.id.username); String returnUsername = data.getStringExtra("userName"); //序列化方式取出实体 User user = (User)data.getSerializableExtra("user"); loginUsername.setText(returnUsername); loginUsername.setSelection(returnUsername.length()); } break; default: } }
Uri uri = Uri.parse("https://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
直接调用打电话功能
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:13207690000"));
startActivity(i);
在使用系统电话功能时需要给当前应用程序授权,在AndroidManifest.xml文件中增加
<uses-permission android:name="android.permission.CALL_PHONE" />
打开地图界面
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
Service
startService()
Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class);
startService(intentService);
生命周期顺序:onCreate->onStartCommand->onDestroy
onCreate() 当Service第一次被创建时调用。
onStartCommand() 当startService方法启动Service时,该方法被调用。
onDestroy() 当Service不再使用时调用。
bindService()
绑定服务首先在我们的服务中创建一个内部类AudioBinder继承Binder来获取当服务实例,然后在onBind方法中返回当前服务的实例。
public class AudioServiceOnBind extends Service implements MediaPlayer.OnCompletionListener{ private final IBinder binder = new AudioBinder(); //用于播放音乐等媒体资源 private MediaPlayer mediaPlayer; public AudioServiceOnBind() { super(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onStartCommand()"); return 0; } @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate(){ super.onCreate(); Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onCreate()"); if (mediaPlayer==null){ mediaPlayer=MediaPlayer.create(this,R.raw.gumeng); mediaPlayer.setOnCompletionListener(this); } mediaPlayer.start(); } @Override public void onCompletion(MediaPlayer mp) { stopSelf(); } @Override public void onDestroy(){ if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } mediaPlayer.release(); stopForeground(true); Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onDestroy()"); } //为了和Activity交互,我们需要定义一个Binder对象 class AudioBinder extends Binder { //返回Service对象 AudioServiceOnBind getService(){ return AudioServiceOnBind.this; } } }
然后在调用的Activity中创建一个ServiceConnection对象重写其中的onServiceConnected方法获取所要绑定的服务。在触发事件的方法中调用bindService实现服务的最终绑定。
public class MainActivity extends AppCompatActivity { private AudioServiceOnBind audioServiceOnBind; //使用ServiceConnection来监听Service状态的变化 private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { audioServiceOnBind = null; } @Override public void onServiceConnected(ComponentName name, IBinder binder) { //这里我们实例化audioService,通过binder来实现 audioServiceOnBind = ((AudioServiceOnBind.AudioBinder) binder).getService(); } }; public void click_music_open(View view) { Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class); bindService(intentService, conn, Context.BIND_AUTO_CREATE); } }
生命周期:onCreate->onBind->onUnBind->onDestroy
onCreate() 当Service被创建时,由系统调用。
onBind() 当bindService方法启动Service时,该方法被调用。
onUnbind() 当unbindService方法解除绑定时,该方法被调用。
onDestroy() 当Service不再使用时,由系统调用。
在service onStartCommand方法中增加以下代码
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onStartCommand()"); Intent nfIntent = new Intent(this, MainActivity.class); Notification.Builder builder = new Notification.Builder(this.getApplicationContext()) .setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) .setSmallIcon(R.mipmap.touxiang) .setContentTitle("wu") .setContentText("Android测试") .setWhen(System.currentTimeMillis()); String CHANNEL_ONE_ID = "com.wu"; String CHANNEL_ONE_NAME = "Channel One"; //安卓8.1以上系统 NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN); notificationChannel.enableLights(false); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(notificationChannel); builder.setChannelId(CHANNEL_ONE_ID); Notification notification = builder.build(); startForeground(1, notification); return super.onStartCommand(intent,flags,startId); } 在调用Activity中调用startForegroundService方法。 public void click_music_open(View view) { Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intentService); } else { startService(intentService); } }
使用IntentService定义的服务,要开启线程,只要重写一个onHandleIntent()方法就可以了,而且在运行完之后会自动停止。
例如数据下载 数据上传等。
public class MyIntentService extends IntentService { private static final String TAG = "MyIntentService"; public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "当前是子线程: " + Thread.currentThread().getId()); //在这里实现异步多线程处理逻辑 //例如数据下载 数据上传等。 Log.d(TAG, "我在后台默默下载数据中。。。。"); } @Override public void onCreate() { Log.d(TAG, "onCreate: "); super.onCreate(); } @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(); } }
Broadcast Receiver
Content Provider
两大视图
主要结构目录
bulid
编译时自生成文件
libs
第三方jar包,会被自动添加到构建路径中去
java
java代码
res
项目中使用的所有资源,drawable存放图片,layout存放布局,values存放字符,mipmap存放图标
Manifest
AndroidManifest.xml配置文件,程序中定义的四大组件都需要在这个文件里注册,另外这个文件中还可以给应用程序添加权限声明
bulid.gradle
项目构建工具,通常有两个bulid.gradle,一个是项目级一个是app级
com.android.application
应用程序模块
com.android.library
库程序模块
区别:一个是可以直接运行的,一个只能作为代码库依附于别的应用程序模块来运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。