My AlarmManager for service update doesn't work

最后都变了- 提交于 2019-12-12 02:45:47

问题


Why my AlarmManager for service update doesn't work? this is my code:

public class MainActivity extends ActionBarActivity {

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

    }
     // Method to start the service
       public void startService() {
          startService(new Intent(getBaseContext(), MyService.class));
       }

       // Method to stop the service
       public void stopService() {
          stopService(new Intent(getBaseContext(), MyService.class));
       }

Service class:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
          // Let it continue running until it is stopped.
          Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
          return START_STICKY;
       }
    @Override
    public void onCreate() {
        poruka();
        super.onCreate();
    }
    public void poruka(){
        Toast.makeText(this, "OnCreate work!", Toast.LENGTH_LONG).show();

    }
}

BroadcastReceiver and AlarmManager:

public class ServiceBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MyService.class));

           Intent alarmIntent = new Intent(context, ServiceBroadcast.class);
           PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
           AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);  
           alarmManager.setRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), 30*1000, pendingIntent);

    }

} And Permissions:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="me.example.nservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" android:enabled="true"
            android:label="Servisv1" />
        <receiver android:name="me.example.rqservice.ServiceBroadcast" android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

I receive message "OnCreate work!" and "Service Started" only first time when i open app. Why update doesn't work every 30 sec?


回答1:


To start a service every 30 seconds :

Inside onCreate() :

Here MyService is the name of the Service.

Intent myService = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
this, 0, myService, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*1000,pendingIntent);

This'll start your service every 30 seconds. However, if the service is already running, which might be the case once it has been started the first time, from next time onwards the call will directly go to onStartCommand() method of the Service and not onCreate().

This is all that you need to do. However, if you want to make sure that the AlarmManager keeps restarting the service even after a phone is restarted, you'll need to add a BroadcastReceiver for Boot.



来源:https://stackoverflow.com/questions/24324693/my-alarmmanager-for-service-update-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!