Android服务每10秒运行一次,产生ANR消息

时间:2014-12-09 07:16:40

标签: java android multithreading android-service

我有一个Android应用程序,它包含一个向用户显示信息的主要活动。

当MainActivity启动时,会创建/启动一项服务,该服务每隔10秒检查一次Web服务 - 如果Web服务结果将显示在通知中。

我使用Intent

从Main活动启动服务
getActivity().startService(new Intent(getActivity(), NotificationService.class));

我也试试这段代码,(但结果相同)。

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        getActivity().startService(new Intent(getActivity(), NotificationService.class));
    }
});

如果服务已启动,我会在Logcat中收到警告

跳过91帧!应用程序可能在其主线程上做了太多工作。

我的通知服务代码,

 public class NotificationService extends Service {
// constant
public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds
long current_times;
String c_time;
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
static HttpClient client = new DefaultHttpClient();
private Timer mTimer = null;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override
public void onCreate() {
    // cancel if already existed

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    SharedPreferences.Editor editor = getSharedPreferences("notification_oldtime", MODE_PRIVATE).edit();

     editor.putInt("old_trail_time", 0);
     editor.putInt("old_sample_time", 0);
     editor.putInt("old_neworder_time", 0);

     editor.commit();

    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}

public long current_time_get(){

    return current_times;

}





class TimeDisplayTimerTask extends TimerTask {

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override
    public void run() {
        // run on another thread

        mHandler.post(new Runnable() {

            @Override
            public void run() {
                // My webservice code for notification
              }

        }
       }

2 个答案:

答案 0 :(得分:1)

尝试使用IntentService代替ServiceIntentService在单独的线程上完成工作。

答案 1 :(得分:0)

serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName className,
                    IBinder binder) {
                try {
                    ((NoteBinder) binder).service.startService(new Intent(
                            MyActivity.this, NotificationService.class));

                    MyLog.d(TAG, "onServiceConnected is called; "
                            + className.getClassName());
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName className) {
                MyLog.d(TAG, "onServiceDisconnected is called; "
                        + className.getClassName());
            }

        };
        bindService(
                new Intent(BaseActivity.this, NotificationService.class),
                serviceConnection, Context.BIND_AUTO_CREATE);

而不是使用启动服务使用绑定服务。