警报管理器示例无效

时间:2015-11-22 08:23:07

标签: android

我试图按照固定时间间隔运行一些代码的示例given here。我使用了这个例子中的代码(将等待时间设置为1分钟),编译它,在我的手机上启动,在手机上停止应用程序,等待几分钟。

但是,没有显示Toast,也没有任何日志条目。我还注意到Service.onStart似乎已经折旧了。

示例中是否缺少某些内容?当应用程序的主要活动开始时,我是否需要注册?或者从我的应用程序中的其他位置调用它?或者清单中可能出现了问题?

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application>
        android:allowBackup="true"
        android:icon="@mipmap/stoxx"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".NewEntryActivity"
            android:label="@string/menu_add"
            android:theme="@style/AppTheme.NoActionBar"
        />
        <activity
            android:name=".UserSettingActivity"
            android:label="@string/menu_add"
            android:theme="@style/AppTheme.NoActionBar"
            />
        <receiver
            android:process=":remote"
            android:name=".Alarm">
        </receiver>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:2)

您忘记在您的清单中加入服务。以下是所有适合我的项目,请查看。

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager.just_me.alarmmanager">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:allowBackup="true" android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher" android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <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=".Alarm"/>
    <service android:name=".MyService"/>
</application>

这是警报:

public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Toast.makeText(context, "com.alarmmanager.just_me.alarmmanager.Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

    wl.release();
}

public void SetAlarm(Context context)
{
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi); // Millisec * Second
}

public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}}

这是服务:

public class MyService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.d("!~!", "Service started.");
        alarm.SetAlarm(this);
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        alarm.SetAlarm(this);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

最后一个MainActivity:

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
}