建设手机银行的网站,手机wordpress清除缓存,沥林网站制作,做简易网站的APP文章目录 Android系统架构Android四大组件ActivityServiceBroadcast ReceiverContent Provider 两大视图主要结构目录 Android系统架构 https://blog.csdn.net/xzzteach/article/details/140904613 Android四大组件 Activity 一个 Activity 包含了用户能够看到的界面#xff0… 文章目录 Android系统架构Android四大组件ActivityServiceBroadcast ReceiverContent Provider 两大视图主要结构目录 Android系统架构 https://blog.csdn.net/xzzteach/article/details/140904613 Android四大组件 Activity 一个 Activity 包含了用户能够看到的界面从而于用户进行交互。一个应用程序中可以有零个或者多个Activity。零个 Activity 就表示这个应用程序不包含与用户交互的界面。Android应用中每一个Activity都必须要在AndroidManifest.xml配置文件中声明注册否则系统将不识别也不执行该Activity。 activityandroid:name.MainActivityandroid:exportedtrueandroid:labelstring/app_nameintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activityActivity的生命周期 Android 是使用任务task来管理Activity的一个任务就是一组存放在栈里Activity的集合这个栈也叫作返回栈。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 是否已经存在在栈中了。每次启动的时候都会创建一个新的实例。一般用于打开邮件之类的。
下图为生成的默认Empty 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 实例。 一般主页面使用购物页面付款页面浏览器主页等
下图为uniapp 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:}
}Intent隐式实现启动其他程序Activity 在当前Activity中调用百度请求界面。
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:nameandroid.permission.CALL_PHONE / 打开地图界面
Uri uri Uri.parse(geo:38.899533,-77.036476);
Intent it new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);Service Service简单介绍
Service它可以在后台执行长时间运行操作而没有用户界面的应用组件不依赖任何用户界面例如后台播放音乐后台下载文件等。虽然服务是在后台运行的但是Service和Activity都是运行在当前APP所在的main threadUI主线程中的而耗时操作如网络请求、拷贝数据、大文件会阻塞主线程给用户带来不好的体验。如果需要在服务中进行耗时操作可以选择 IntentServiceIntentService是Service的子类用来处理异步请求。
Service启动的两种方式
startService() 在Acitivity界面通过显式意图或隐式意图的方式来启动服务和关闭服务。
Intent intentService new Intent(MainActivity.this, AudioServiceOnBind.class);
startService(intentService);生命周期顺序onCreate-onStartCommand-onDestroy
onCreate() 当Service第一次被创建时调用。
onStartCommand() 当startService方法启动Service时该方法被调用。
onDestroy() 当Service不再使用时调用。
bindService()绑定服务 当应用组件通过调用 bindService() 绑定到服务时服务即处于“绑定”状态。绑定服务提供了一个客户端-服务器接口允许组件与服务进行交互、发送请求、获取结果。 仅当与另一个应用组件绑定时绑定服务才会运行。 多个组件可以同时绑定到该服务但全部取消绑定后该服务即会被销毁。
首先在我们的服务中创建一个内部类AudioBinder继承Binder来获取当服务实例然后在onBind方法中返回当前服务的实例。
public class AudioServiceOnBind extends Service implements MediaPlayer.OnCompletionListener{private final IBinder binder new AudioBinder();//用于播放音乐等媒体资源private MediaPlayer mediaPlayer;public AudioServiceOnBind() {super();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(AudioServiceOnBind.this.getClass().getName(),执行onStartCommand());return 0;}Overridepublic IBinder onBind(Intent intent) {return binder;}Overridepublic void onCreate(){super.onCreate();Log.d(AudioServiceOnBind.this.getClass().getName(),执行onCreate());if (mediaPlayernull){mediaPlayerMediaPlayer.create(this,R.raw.gumeng);mediaPlayer.setOnCompletionListener(this);}mediaPlayer.start();}Overridepublic void onCompletion(MediaPlayer mp) {stopSelf();}Overridepublic 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() {Overridepublic void onServiceDisconnected(ComponentName name) {audioServiceOnBind null;}Overridepublic 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 Android 8.0 后系统不允许后台应用创建后台服务。 因此Android 8.0 引入了一种全新的方法即 Context.startForegroundService()以在前台启动新服务。
在service onStartCommand方法中增加以下代码 Overridepublic 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 由于Service中的逻辑都是执行在主线程中的此时如果在Service中处理一下耗时操作可能就会给用户带来不好的体验。所以此时就可以使用Android多线程Service. 本质上IntentService是在Service里开启线程去做任务处理。IntentService会在任务执行完成后自行结束自己而不需要外部去调用stopService了。
使用IntentService定义的服务要开启线程只要重写一个onHandleIntent()方法就可以了而且在运行完之后会自动停止。
例如数据下载 数据上传等。
public class MyIntentService extends IntentService {private static final String TAG MyIntentService;public MyIntentService() {super(MyIntentService);}Overrideprotected void onHandleIntent(Intent intent) {Log.d(TAG, 当前是子线程 Thread.currentThread().getId());//在这里实现异步多线程处理逻辑//例如数据下载 数据上传等。Log.d(TAG, 我在后台默默下载数据中。。。。);}Overridepublic void onCreate() {Log.d(TAG, onCreate: );super.onCreate();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, onStartCommand: );return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.d(TAG, onDestroy: );super.onDestroy();}
}Broadcast Receiver Content Provider 两大视图 projectandroid 主要结构目录 bulid 编译时自生成文件 libs 第三方jar包会被自动添加到构建路径中去 java java代码 res 项目中使用的所有资源drawable存放图片layout存放布局values存放字符mipmap存放图标 Manifest AndroidManifest.xml配置文件程序中定义的四大组件都需要在这个文件里注册另外这个文件中还可以给应用程序添加权限声明 bulid.gradle 项目构建工具通常有两个bulid.gradle,一个是项目级一个是app级 com.android.application 应用程序模块 com.android.library 库程序模块
区别一个是可以直接运行的一个只能作为代码库依附于别的应用程序模块来运行