我对服务更新AlarmManager不起作用 [英] My AlarmManager for service update doesn't work

查看:878
本文介绍了我对服务更新AlarmManager不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的服务更新AlarmManager不起作用?这是我的code:

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));
       }

服务类:

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();

    }
}

广播接收器和AlarmManager:

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>

我收到消息的OnCreate的工作!和服务启动只有第一次,当我打开应用程序。为什么更新不会每30秒工作?

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

推荐答案

要启动服务,每30秒:

To start a service every 30 seconds :

的onCreate()

下面的MyService是服务的名称。

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);

这将开始为您服务每30秒。但是,如果服务已在运行,这可能是这种情况一旦被启动的第一次,从下一次开始通话将直接转到 onStartCommand()方法本服务的,而不是的onCreate()

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().

这是所有你需要做的。但是,如果你想确保AlarmManager不断重新启动手机即使重新开始服务,你需要添加一个BroadcastReceiver的引导。

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.

这篇关于我对服务更新AlarmManager不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆