警报接收不起作用

时间:2015-06-08 09:32:43

标签: android android-intent android-service android-pendingintent android-broadcast

我正在制作闹钟接收器,但不会触发闹钟。

这是我的代码:

在MainActivity中启动闹钟:

private void setupAlarm(){
    Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
    PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);       
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);

    Log.e("setupAlarm", "Setup alarm complete");
}

清单:

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

package="com.phonelogger"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="SettingActivity" android:label="@string/action_settings" ></activity>

    <service android:name="com.logdata.LogManager" />

    <receiver 
        android:name="com.logdata.AlarmReceiver" 
        android:enabled="true">                                    
    </receiver>
</application>

AlarmReceiver:

package com.logdata;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent myIntent = new Intent(context, com.logdata.LogManager.class);
        context.startService(myIntent);

        Log.e("AlarmReceiver", "Get message");      
    }

}

有人可以告诉我这是什么问题吗?

2 个答案:

答案 0 :(得分:2)

您的Intent用于明确启动AlarmReceiverBroadcastReceiver。因此,您需要使用getBroadcast()而不是getService()来创建PendingIntent对象。

替换

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

PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);  

试试这个。这会奏效。

答案 1 :(得分:1)

尝试使用它。它会为你工作

public async Task<bool> AllowAccessAsync(string authenticatedUserToken)

}