带有BroadcastReceiver的报警管理器

时间:2017-11-18 13:51:10

标签: android broadcastreceiver alarmmanager

这是我的第一个申请,我试图让Alarm managerBroadcastReceiver合作。

我在SetFutureStartTime()触发Alarm manager时调用函数onReceive(),但是到时候,没有任何反应。

这是代码:

public void SetFutureStartTime(int startHour, int startMin)
{
    Context context = this;

    m_alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver_SendOn.class);
    intent.setAction("Broadcast");
    m_alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    // Set the alarm to start at 21:32 PM
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, startHour);
    calendar.set(Calendar.MINUTE, startMin);

    m_alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), m_alarmIntent);
}

public class AlarmReceiver_SendOn extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        SendTcpMessage(TURN_ON_COMMAND); //I put here break point but nothing happens
    }
}

这是manifast的结尾:

 .....
 </activity>
    <receiver android:name=".MainActivity$AlarmReceiver_SendOn">
        <intent-filter>
            <action android:name="Broadcast">
            </action>
        </intent-filter>
    </receiver>
</application>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

这段代码对我有用: 这个MainActivity

public class MainActivity extends AppCompatActivity {

    private AlarmManager m_alarmMgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            SetFutureStartTime(19, 25);
    }

    public void SetFutureStartTime(int startHour, int startMin)
    {
        Context context = this;

        m_alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, com.asb.test.testapplication.AlarmReceiver_SendOn.class);
        PendingIntent m_alarmIntent = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), intent, 0);

        // Set the alarm to start at 21:32 PM
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, startHour);
        calendar.set(Calendar.MINUTE, startMin);

        m_alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), m_alarmIntent);
    }

}

这是AlarmReceiver_SendOn

public class AlarmReceiver_SendOn extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("TAG", "Started >>>>>>>");
            //and doing something

        }
    }

这个AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.asb.test.testapplication">
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".AlarmReceiver_SendOn">
        </receiver>

    </application>
</manifest>