0

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

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

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

    </application>

    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

</manifest>

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.widget.Toast;
import com.example.enabledisablebroadcastreceiver.R;

public class EnableDisableBroadcastReceiver extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnExit=(Button)findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }           
        }); 

    }

   public void enableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
   }

   public void disableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
   }     

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
   android:orientation="vertical">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/enable_broadcast_receiver"
        android:onClick="enableBroadcastReceiver"
        tools:context=".EnableDisableBroadcastReceiver" />
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="@dimen/padding_medium"
       android:text="@string/disable_broadcast_receiver"
       android:onClick="disableBroadcastReceiver"
       tools:context=".EnableDisableBroadcastReceiver" />

   <Button
        android:id="@+id/btnExit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Exit" 
   />

</LinearLayout>
4

0 回答 0