android服务导致ANR

tur*_*boy 1 service android broadcastreceiver

我有一个应用程序,从启动接收器启动一个服务,定期从手机的Sqlite DB上的表中删除行.BootReceiver工作正常.它实例化的服务有一个infinate循环,由Thread.sleep暂停30秒(用于测试),最终它将设置为每隔几小时左右.它确实删除了行,但我不相信它是每30秒,最终它会导致ANR.所有实施的方式似乎有点粗糙.有没有更好的方法来实现我正在做的事情?如果不是,我怎么能停止ANR,因为我认为服务不必是异步,只是网络呼叫等.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBootReceiver extends BroadcastReceiver {   

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

     Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
     myIntent.setAction("com.carefreegroup.startatboot.MyService");
     context.startService(myIntent);


    }

}
Run Code Online (Sandbox Code Playgroud)

.

import org.joda.time.DateTime;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.util.Log;

public class SendOutstandingTransactions extends Service {

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;
    Cursor c;

    @Override
    public void onCreate() {
        Log.e(TAG, "inside onCreate of SendOutstandingTransactions");
        nfcscannerapplication = (NfcScannerApplication)getApplication();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "inside onDestroy of SendOutstandingTransactions");

        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e(TAG, "inside onStart of SendOutstandingTransactions");

        do{
        DateTime now = new DateTime();
        //nfcscannerapplication.loginValidate.deleteTransactionsOlderThanThreeDays(now);
        nfcscannerapplication.loginValidate.deleteTableTransactions();
        Log.e(TAG, "just called housekeeping method in service");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }while(true); 




    }// end of onStart

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
Run Code Online (Sandbox Code Playgroud)

.

在清单中

<service android:name=".SendOutstandingTransactions" >
            <intent-filter>
                <action android:name="com.carefreegroup.startatboot.MyService" />
            </intent-filter>
        </service>

        <receiver
            android:name=".MyBootReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
Run Code Online (Sandbox Code Playgroud)

[edit1]我如何启动服务

// get a Calendar object with current time
             Calendar cal = Calendar.getInstance();
             // add 5 minutes to the calendar object
             cal.add(Calendar.MINUTE, 1);
             Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
             intent.putExtra("alarm_message", "sending outstanding transactions");
             // In reality, you would want to have a static variable for the request code instead of 192837
             PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

             // Get the AlarmManager service
             AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
             //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
             //86400000 = 24 hours
             //43200000 = 12 hours
             //3600000 = 1hr
             //1800000 = 30 mins
            // 600000 = 10 mins
             //300000 = 5 mins

             am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300000 , sender);
Run Code Online (Sandbox Code Playgroud)

alarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

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


   try {

     Bundle bundle = intent.getExtras();
     String message = bundle.getString("alarm_message");
    // Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
     myIntent.setAction("com.carefreegroup.rr3.startatboot.MyService");
     context.startService(myIntent);

    } catch (Exception e) {
     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
     e.printStackTrace();

    }
 }

}
Run Code Online (Sandbox Code Playgroud)

服务

 public class SendOutstandingTransactions extends IntentService {

        private static final String TAG = SendOutstandingTransactions.class.getSimpleName();

instance variables

        @Override
        public void onCreate() {
            super.onCreate();


        }





        @Override
        protected void onHandleIntent(Intent intent) {

    //do something

        }//end of onHandleIntent




        public SendOutstandingTransactions() {
            super("SendOutstandingTransactions");

        }
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 6

请注意,服务与其他应用程序对象一样,在其托管进程的主线程中运行.这意味着,如果您的服务要进行任何CPU密集型(例如MP3播放)或阻止(例如网络)操作,它应该生成自己的线程来执行该工作.有关此内容的更多信息,请参见进程和线程.IntentService类可用作Service的标准实现,它具有自己的线程,用于调度其要完成的工作.

资料来源:http://developer.android.com/reference/android/app/Service.html

解:

将代码移动到专用线程.这可以通过使用例如AsyncTask类来完成.