gpt4 book ai didi

java - 警报接收器和 Android list

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:06:05 25 4
gpt4 key购买 nike

我找到了一些关于设置警报接收器以按设定的时间间隔发送 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 list 如下所示:

<?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 类,我需要使用新接收器编辑 list 。但我对 XML 还很陌生,不知道该往哪个方向走。

最佳答案

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

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

关于java - 警报接收器和 Android list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10440012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com