报警管理器不起作用?

时间:2015-09-28 07:21:27

标签: android service alarmmanager background-service

我只是想尝试实现AlarmManager。我为警报管理器编写了代码,但代码不起作用。 AlarmManager不会触发广播接收器和服务。但是,当我不使用AlarmManager时,只需使用intent启动服务即可运行。如何使AlarmManager定期调度服务? 以下是代码:

MainActivity.java

package com.alarmmanager;

import android.app.*;
import android.os.Bundle;
import android.content.*;


public class MainActivity extends Activity {

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

        Intent intent = new Intent(this,AlarmReceiver.class);
        PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),15000,pending);       
    }
}   

AlarmReceiver.java

package com.alarmmanager;

import android.content.*;


public class AlarmReceiver extends BroadcastReceiver 
{
    public void onReceive(Context context, Intent intent)
    {
        intent =new Intent(context,MainService.class);
        context.startService(intent);

    }
}

MainService.java

package com.alarmmanager;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service 
{

    public IBinder onBind(Intent intent) {

        return null;
    }

     public void onCreate() {

         Log.i("nilavs","nilav");
         Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        }


}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alarmmanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service
            android:name=".MainService"
            android:enabled="true"
        />

        <activity
            android:name=".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>

        <receiver android:process=":remote" android:name=".AlarmReceiver">

</receiver>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您应该使用getBroadcast代替getService

PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);

修改:

尝试将其设置为最初启动 - 例如

alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,15000,pending);