java 警报接收器和 Android 清单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10440012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:03:42  来源:igfitidea点击:

Alarm Receiver and Android Manifest

javaandroidbroadcastreceiver

提问by kprasad89

I've found several tutorials about setting the alarm receiver to send a toast message in set intervals. and i've been following the code and broken down my own project into 3 classes.

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

the HelloDroidActivity.java is:

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);

}
}

also the MyAlarmReciever.java(i am already aware of the spelling mistake on the name):

还有 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();
    }
}

and the Android Manifest which looks like this :

和 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>

I have read that in order to get the project to receive my alarmReceiver java class I need to edit the manifest with a new receiver. but I'm fairly new to XML and dont know which direction to take.

我已经读过,为了让项目接收我的 alarmReceiver java 类,我需要用新的接收器编辑清单。但我对 XML 还很陌生,不知道该往哪个方向发展。

回答by Gaurav Sarma

There is already a receiver that you have defined in your manifest file. But the name is not correct see the name needs to be the full class name i.e the package.RecieverName. And in your case the name of your receiver is MyAlarmReciever. So the receiver will be defined as follows

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

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

回答by Diiiiii

In your manifest, the receiver is listening to an action called com.example.helloandroid.alarms. But in your HelloDroidActivity.javathere is not such action is added to the intent.

在您的清单中,接收器正在侦听名为 的操作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");
  //....
  }
}