What kind of technology need I use if the function doWork will run a long time

General Tech Technology & Software 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Technology & Software related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

The function doWork maybe run 20s, I think the system will cause ANR problem. I don't know what kind of technology I need use to prevent ANR, AyncTask, local server, thread, IntentService or others. Could you give me some sample codes? Thanks!

AlarmManagerBroadcastReceiver.java

package com.code4reference.enabledisablebroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

  private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

  @Override
  public void onReceive(Context context, Intent intent) {   
     doWork(context,intent);         
  } 

  private void doWork(Context context, Intent intent){      
     //It will running a long time.
  } 

}

AndroidManifest.xml

 xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.enabledisablebroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
            android:name="com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver"
            android:label="@string/title_activity_enable_disable_boradcast_receiver" >
            <intent-filter>
                 android:name="android.intent.action.MAIN" />
                 android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        

        
         android:name="com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver" >
            <intent-filter>
                android:name="android.provider.Telephony.SMS_RECEIVED" />
            intent-filter>
        

    

     android:name="android.permission.SEND_SMS"/>
     android:name="android.permission.RECEIVE_SMS"/>

EnableDisableBroadcastReceiver.java

package com.code4reference.enabledisablebroadcastreceiver;
import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.