报警接收器和Android清单

时间:2012-05-03 22:10:28

标签: java android broadcastreceiver

我找到了几个关于设置警报接收器以设置间隔发送Toast消息的教程。我一直在关注代码并将我自己的项目分解为3个类。

HelloDroidActivity.java是:

package com.example.helloandroid;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.helloandroid.alarms.MyAlarmReciever;

public class HelloDroidActivity extends Activity {
/** Called when the activity is first created. */

public static int RTC_WAKEUP;
public static long INTERVAL_FIFTEEN_MINUTES;

private AlarmManager alarmMgr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("Hello, Krishneel");
    setContentView(tv);
    Toast.makeText(this, "Alarm went off", Toast.LENGTH_SHORT).show();

    Log.d("OnCreate", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, MyAlarmReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), 7000, pendingIntent);

}
}

也是MyAlarmReciever.java(我已经知道名字上的拼写错误):

package com.example.helloandroid.alarms;

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

public class MyAlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Log.e("onReceive", "ladskjflsakjdflskjdflskjdfslkjdflasdf");
        Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
    }
}

和Android Manifest看起来像这样:

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

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.helloandroid.HelloDroidActivity"
            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:name="AlarmReceiver">
            <intent-filter>
                <action android:name="com.example.helloandroid.alarms" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我已经读过为了让项目接收我的alarmReceiver java类,我需要使用新的接收器编辑清单。但我对XML很新,不知道要采取哪个方向。

2 个答案:

答案 0 :(得分:4)

您已在清单文件中定义了一个接收器。但名称不正确,请看名称需要是完整的类名,即package.RecieverName。在您的情况下,您的接收器的名称是MyAlarmReciever。所以接收器定义如下

<receiver android:name=".alarms.MyAlarmReciever">
    <intent-filter>
        <action android:name="com.example.helloandroid.alarms" />
    </intent-filter>
</receiver>

答案 1 :(得分:0)

在你的清单中,接收者正在收听一个名为com.example.helloandroid.alarms的动作。但是在HelloDroidActivity.java中,意图中没有添加此类操作。

public class HelloDroidActivity extends Activity {
//....
  @Override
  public void onCreate(Bundle savedInstanceState) {
  //....
  Intent intent = new Intent(this, MyAlarmReciever.class);
  intent.setAction("com.example.helloandroid.alarms");
  //....
  }
}