gpt4 book ai didi

android - 广播接收器和警报管理器

转载 作者:行者123 更新时间:2023-11-29 18:07:47 25 4
gpt4 key购买 nike

您好,我正在用 android 构建一个新闻应用程序,因此每次将新数据输入数据库时​​,用户都必须在通知栏中收到通知。所以经过研究我发现我必须使用广播接收器和警报管理器。但我无法理解的是,当应用程序关闭时,它如何知道输入了新数据?以及如何让警报管理器激活应用程序以检查新数据,比方说每 5 分钟一次?谢谢。

最佳答案

这个项目可以在使用AlarmManager设置时间后每10000秒显示一次通知。

AlarmActivityActivity.java:

package co.in.technoguys.AlarmActivity;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class AlarmActivityActivity extends Activity {
int notifyID=1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
public void startAlert(View view) {
TimePicker tp=(TimePicker)findViewById(R.id.timePicker1);
DatePicker dp=(DatePicker)findViewById(R.id.datePicker1);
Calendar Cal = Calendar.getInstance();
Cal.set(Calendar.YEAR, dp.getYear());
Cal.set(Calendar.MONTH, dp.getMonth());
Cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
Cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
Cal.set(Calendar.MINUTE, tp.getCurrentMinute());
Cal.set(Calendar.SECOND, 00);
long when=Cal.getTimeInMillis();
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,when,20000,pendingIntent);
Toast.makeText(this, "Notification Triggered on"+tp.getCurrentHour()+":"+tp.getCurrentMinute(),
Toast.LENGTH_LONG).show();

}
}

MyBroadcastReceiver.java:

package co.in.technoguys.AlarmActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {
int notifyID=1;

@Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent();
PendingIntent pi=PendingIntent.getBroadcast(context,0,i,0);
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Notification Triggered",System.currentTimeMillis());
CharSequence from="Remainder";
CharSequence message="Meeting Starts at 9 A.M";
notify.setLatestEventInfo(context, from, message, pi);
nm.notify(notifyID,notify);
}

}

ma​​in.xml:

        <TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />

<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/timePicker1"
android:layout_alignLeft="@+id/timePicker1"
android:layout_marginBottom="27dp" />

<Button
android:id="@+id/startAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_below="@+id/timePicker1"
android:layout_marginTop="36dp"
android:onClick="startAlert"
android:text="@string/count" />

</RelativeLayout>

AndroidManifest.xml:

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

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AlarmActivityActivity"
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="MyBroadcastReceiver" >
</receiver>
</application>

</manifest>

关于android - 广播接收器和警报管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512366/

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