【问题标题】:Starting service on boot to to do work every 15 minutes在启动时启动服务以每 15 分钟完成一次工作
【发布时间】:2015-08-30 14:52:14
【问题描述】:

所以,我已经通过互联网寻找解决方案,但没有找到任何结合我需要的东西。

我需要这个: 如果手机重新启动,我需要在启动时启动一项服务,该服务将每 15 分钟向我的服务器发送数据。我编写了可用于启动的代码,但不知道如何实现计时器。 我需要 2 个广播接收器和 2 个服务吗?

我的广播接收器:

public class BrReceiver extends BroadcastReceiver {

    final public static String ONE_TIME = "onetime";

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

            Intent serviceIntent = new Intent(context, Service.class);
            context.startService(serviceIntent);
        }

    }

}

和我的服务:

public class Service extends IntentService {

    private static final String TAG = "DownloadService";

    public Service() {
        super(Service.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d(TAG, "Service Started!");


    }


}

还有我的 AndroidManifest:

<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
        <receiver android:name=".services.BrReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

          <service
            android:name=".services.Service"
            android:exported="false" />

这项每 15 分钟重复一次的服务不能从 Activity 开始,而是在启动时开始。

【问题讨论】:

    标签: android service timer broadcastreceiver android-intentservice


    【解决方案1】:

    定期启动服务的最佳方法是使用警报管理器,如下所示:

    // 使用AlarmManager启动服务

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        Intent intent = new Intent(Main.this, Service_class.class);
        PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
                0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                36000 * 1000, pintent);
    
        // click listener for the button to start service
        Button btnStart = (Button) findViewById(R.id.button1);
        btnStart.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                startService(new Intent(getBaseContext(), Service_class.class));
    
            }
        });
    
        // click listener for the button to stop service
        Button btnStop = (Button) findViewById(R.id.button2);
        btnStop.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                stopService(new Intent(getBaseContext(), Service_class.class));
            }
        });
    }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多