服务未按预定义的时间间隔运行

时间:2014-07-04 07:31:54

标签: android

我已定义REPEAT_TIME = 15 * 1000;,但消息会在3秒左右显示。 我的主要活动:

public class MainActivity extends ListActivity {
    private LocalWorldService s;

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

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent(this, LocalWorldService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(mConnection);
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder binder) {
            LocalWorldService.MyBinder b = (LocalWorldService.MyBinder) binder;
            s = b.getService();
            Toast.makeText(MainActivity.this, "Connected", Toast.LENGTH_SHORT)
                    .show();
        }

        public void onServiceDisconnected(ComponentName className) {
            s = null;
            Toast.makeText(MainActivity.this, "Disconnected", Toast.LENGTH_SHORT)
            .show();
        }
    };

我的服务:

public class LocalWorldService extends Service {

    private final IBinder mBinder = new MyBinder();
    private ArrayList<String> list = new ArrayList<String>();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        system.out.println("Message");
        return Service.START_NOT_STICKY;
    }

@Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("On destroy used");
    }

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

    public class MyBinder extends Binder{
        LocalWorldService getService(){
            return LocalWorldService.this;
        }
    }

    public List<String> getWordList(){
        return list;
    }

以下是BroadCastReceiver的两个类:

public class MyScheduleReceiver extends BroadcastReceiver{

    private static final long REPEAT_TIME = 15 * 1000;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, MyStartServiceReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast( context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 15);
        service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
    }

public class MyStartServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent service = new Intent(context, LocalWorldService.class);
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(context);
        Map<String, ?> x = pref.getAll();
        boolean momoMagnetIsOn = (Boolean) x.get("key_momo_magnet");
        if (momoMagnetIsOn){
            context.startService(service);
        }
        else{
            context.stopService(service);
        }
    }

2 个答案:

答案 0 :(得分:0)

嗯,你注意到你实际告诉它是inexact吗?您应该使用setExact()

答案 1 :(得分:0)

好的,我自己拿到了。因为我关注this

我甚至不知道为什么要重启系统。但是重新启动系统改变了时间间隔。我以前用REPEAT_TIME = 3 * 1000重新启动了手机,因此每3秒显示一次消息。任何解释都会受到欢迎。我是这个话题的新手。谢谢..