赞
踩
1. Application的生命周期
1.1 onCreate时的操作
1.2 onTerminate时的操作
1.3 onLowMemory时的操作
1.4 onTrimMemory时的操作
2. Application的应用
2.1 BaseApplication的封装
3 上下文不同场景的使用
3.1上下文为Application的场景
3.2上下文为Activity的场景
3.3上下文为Service的场景
3.4上下文为ContentProvider的场景
3.5上下为BroadcastReceiver的场景
4. 保活进阶
4.1利用 Activity 提升权限
4.2利用Notification 提升权限
4.3利用系统广播拉活
4.4利用第三方应用广播拉活
4.5利用系统Service机制拉活
4.6利用Native进程拉活
4.7利用 JobScheduler 机制拉活
4.8利用账号同步机制拉活
4.9 A/B Test保活
Application和Activity,Service一样,是Android框架的一个系统组件,当Android程序启动时系统会创建一Application对象,用来储存系统的一些信息。
通常我们是不需要指定一个Application的,这时系统会自动帮我们创建,如果需要创建自己的Application,也很简单。
创建一个类AppApplication继承Application并在AndroidManifest的application标签中进行注册。
Android系统会为每个程序运行时创建一个Application类的对象且仅创建一个,所以Application可以说是单例模式的一个类。
且Application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期。因为他是全局的单例的,所以在不同的Activity,Service中获得Application对象都是同一个对象。
所以可以通过Application来进行一些,数据传递,数据共享,数据缓存等操作
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void intentapp(View view) {
Intent intent=getIntent();
MyApp myApp=(MyApp) getApplication();
Map<String,Object> stringObjectMap=myApp.mapList.get(0);
String name=stringObjectMap.get("name").toString();
Log.i("---name","intentapp: "+name);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void intentapp(View view) {
Intent intent=new Intent(this,Main2Activity.class);
MyApp myApp=(MyApp) getApplication();
Map<String,Object> stringObjectMap=new HashMap<>();
stringObjectMap.put("name","廉三");
stringObjectMap.put("age",1000);
stringObjectMap.put("money",20.2f);
myApp.mapList.add(stringObjectMap);
startActivity(intent);
}
}
package com.example.day17;
import android.app.Application;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MyApp extends Application {
List<Map<String,Object>> mapList=new ArrayList<>();
}
<?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=".MainActivity">
<Button
android:text="intent传值"
android:onClick="intentapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<?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=".Main2Activity">
<Button
android:text="接受intent"
android:onClick="intentapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
String data;
MediaPlayer mediaPlayer;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.READ_SMS,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.READ_CALL_LOG
},991);
ContentResolver contentResolver=getContentResolver();
Cursor cursor=contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
if (cursor!=null)
{
while (cursor.moveToNext())
{
String title=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
data=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
String artisr=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String size=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
String duration=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
String album_id=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
}
}
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(data);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。