我的警报管理器用于服务更新不起作用



为什么我的服务更新AlarmManager不工作?这是我的代码:

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

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

}和权限:

<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工作!"one_answers"服务启动"只有第一次当我打开应用程序。为什么更新不工作每30秒?

每30秒启动一个服务:

Inside onCreate():

这里MyService是服务的名称。

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

这就是你所需要做的。但是,如果您想确保在手机重新启动后AlarmManager仍然重新启动服务,则需要为Boot添加BroadcastReceiver。

相关内容

  • 没有找到相关文章

最新更新