gpt4 book ai didi

android - 将来在 android 中安排通知警报

转载 作者:行者123 更新时间:2023-11-30 00:52:48 25 4
gpt4 key购买 nike

我是 Android 开发的新手,并且已经开发了我的第一个应用程序。

我想向从数据库日期开始在后台安排的用户显示通知。我遵循了 Google 上的许多教程,这就是我所做的。

我在 Main Activity 和广播接收器类中创建了 setAarm() 函数。

MainActivity.java

package com.example.myapp;

import ...


public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

TextView navigationHeaderUserName;
DatabaseHelper myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

myDB = new DatabaseHelper(this);

View header = navigationView.getHeaderView(0);
navigationHeaderUserName = (TextView) header.findViewById(R.id.drawer_layout_user_name);

// display dashboard when the activity is loaded
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new ActivityDashboard());
ft.commit();

// set alarm
setAlarm();
}

public void setAlarm() {

// notification service
boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
if (alarm) {
Intent intentAlarm = new Intent("ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0);
Calendar calendarAlarm = Calendar.getInstance();
calendarAlarm.setTimeInMillis(System.currentTimeMillis());
calendarAlarm.add(Calendar.SECOND, 3);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarAlarm.getTimeInMillis(), 6000, pendingIntent);
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

// calling the method displaySelectedScreen()
displaySelectedScreen(item.getItemId());

return true;
}

private void displaySelectedScreen(int itemId) {

.... // some code
.... // some code
}
}

ActivityAlarmReceiver.java

    package com.example.myapp;

import ...

public class ActivityAlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
try {

DatabaseHelper db = new DatabaseHelper(context);
String nextDate = db.getNextDate();

if (nextDate == null) {
return;
}

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat stf = new SimpleDateFormat("HH:MM:SS");

String nextTime = "08:00:00";

Date dateFormat = sdf.parse(nextDate);
Date timeFormat = stf.parse(nextTime);

Date today = new Date();

if (dateFormat.equals(today)) {
Intent intent1 = new Intent(context, MainActivity.class);
createNotification(context, intent1, "New Message", "body!", "This is alarm");
}
} catch (Exception e) {
Log.i("date", "error == " + e.getMessage());
}
}

private void createNotification(Context context, Intent intent1, String ticker, String title, String description) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setTicker(ticker);
builder.setContentTitle(title);
builder.setContentText(description);
builder.setSmallIcon(R.drawable.my_time_logo_transparent);
builder.setContentIntent(pendingIntent);

Notification n = builder.build();

// create the notification
n.vibrate = new long[]{150, 300, 150, 400};
n.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(R.drawable.my_time_logo_transparent, n);

// create a vibration
try {
Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone toque = RingtoneManager.getRingtone(context, som);
toque.play();
} catch (Exception e) {

}
}
}

但这行不通...我敢肯定,我遗漏了一些东西但找不到什么??

最佳答案

link (安排重复警报)可以帮助您。您必须将以下代码添加到您的 list 中:

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:process=":remote" android:name=".ActivityAlarmReceiver"></receiver>

关于android - 将来在 android 中安排通知警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691900/

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