Android - page switching, Daniel will teach you

(4)singleInstance: an Activity stack will be created separately;

8. Optimization of program lock

  1. Put the variables created every time in the while loop outside the loop and create them only once

    //Before modification
    
    while(flag){
    
        List<RunningTaskInfo> infos = am.getRunningTasks(1000);
    
        String packname = infos.get(0).topActivity.getPackageName();
    
    
    
    //After modification
    
    List<RunningTaskInfo> infos;
    
    String packname;
    
    while(flag){
    
        infos = am.getRunningTasks(1000);
    
        packname = infos.get(0).topActivity.getPackageName();
    
    
    
    
  2. When getting running applications, you only need to get the first one, and set the collection size to 1

    //Before modification
    
    infos = am.getRunningTasks(1000);
    
    
    
    //After modification
    
    infos = am.getRunningTasks(1);
    
    
    
    
  3. It is time-consuming to query the database every time. Query the contents of the database and put them into a collection. In the future, query the collection data in memory

    //Before modification
    
    if(dao.find(packname)){//Query the database every time
    
    
    
    //After modification
    
    1.stay ApplockDao Add a method to query all locked applications
    
    public List<String> findAll() {
    
        List<String> lockedPacknames = new ArrayList<String>();
    
        SQLiteDatabase db = helper.getReadableDatabase();
    
        Cursor cursor = db.rawQuery("select packname from lockinfo", null);
    
        while (cursor.moveToNext()) {
    
            lockedPacknames.add(cursor.getString(0));
    
        }
    
        cursor.close();
    
        db.close();
    
        return lockedPacknames;
    
    }
    
    2.Declare a member variable, onCreate Method to query all data
    
    private List<String> lockedPacknames;
    
    
    
    @Override
    
    public void onCreate() {
    
        ...
    
        ...
    
        //All package names locked by the database are obtained 
    
        lockedPacknames = dao.findAll();
    
        ...
    
        ...
    
    }
    
    
    
    3.When querying, directly query the data in memory
    
    if(lockedPacknames.contains(packname)){//Query memory
    
    
    
    

9. How to use content watchers to synchronize database changes

  1. In the add and delete methods in AppLockDao, add the notification of modifying the database

    //Add or delete a record
    
    Uri uri = Uri.parse("content://com.mythmayor.project.applockdb");
    
    context.getContentResolver().notifyChange(uri, null);
    
    
    
    
  2. Register a content observer in WatchDogService

    //Registered content watchers observe changes in database content
    
    Uri uri = Uri.parse("content://com.mythmayor.project.applockdb");
    
    observer = new MyObserver(new Handler());
    
    getContentResolver().registerContentObserver(uri, true, observer);
    
    
    
    //Content observer
    
    private class MyObserver extends ContentObserver{
    
    
    
        public MyObserver(Handler handler) {
    
            super(handler);
    
        }
    
    
    
        //Method invoked when the content observer observes data changes
    
        @Override
    
        public void onChange(boolean selfChange) {
    
            Log.i(TAG,"The content observer in the watchdog observed changes in the database....");
    
            lockedPacknames = dao.findAll();
    
            super.onChange(selfChange);
    
        }
    
    
    
    }
    
    
    
    

In the above method, we re query all the data when observing the changes in the database, which is not optimized enough. In fact, we only operate on one piece of data when adding or deleting, and we can use uri to notify the data of add or delete

  1. In the add and delete methods in AppLockDao, add the notification of modifying the database. The operation and the package name will be brought out during the notification

    //Add data
    
    Uri uri = Uri.parse("content://com.mythmayor.project.applockdb?add="+packname);
    
    context.getContentResolver().notifyChange(uri, null);
    
    
    
    //Delete data
    
    Uri uri = Uri.parse("content://com.mythmayor.project.applockdb?delete="+packname);
    
    context.getContentResolver().notifyChange(uri, null);
    
    
    
    
  2. Register content observers in WatchDogService and make different operations according to different URIs

    private class MyObserver extends ContentObserver{
    
    
    
        public MyObserver(Handler handler) {
    
            super(handler);
    
        }
    
    
    
        @Override
    
        public void onChange(boolean selfChange, Uri uri) {
    
            // TODO Auto-generated method stub
    
            String query = uri.getQuery();
    
    
    
    

Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code
Network disk: pan.baidu.com/s/1uXQ5fqJkNbGiaj5iXGzdrQ
Extraction code: sei4

Epilogue

Based on salary and development as the ultimate goal, we should seek the best development in places with high salary!

**[CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code](

)**

Here are some advanced materials sorted out by several Android industry leaders corresponding to the above technical points.

Frequency + big factory interview questions + project actual combat source code**

Network disk: pan.baidu.com/s/1uXQ5fqJkNbGiaj5iXGzdrQ
Extraction code: sei4

Epilogue

Based on salary and development as the ultimate goal, we should seek the best development in places with high salary!

**[CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code](

)**

Here are some advanced materials sorted out by several Android industry leaders corresponding to the above technical points.

[external chain picture transferring... (img-0TyPszch-1631422741154)]

Keywords: Android Database SQLite Design Pattern Programmer

Added by The_Walrus on Sat, 20 Nov 2021 04:34:50 +0200