gpt4 book ai didi

android: NotificationManager 无法解析为变量

转载 作者:行者123 更新时间:2023-11-29 21:58:09 25 4
gpt4 key购买 nike

请帮助我,我是 android 的新手,我需要为我的闹钟添加通知声音。我可以在哪里放置通知声音?这是我的带有通知的代码,但出现错误“NotificationManager 无法解析为变量”

应用程序强制关闭现在我不知道为什么请帮助,这是日志猫

10-02 06:03:38.785: W/dalvikvm(938): threadid=1: thread exiting with uncaught exception (group=0x40015560) 10-02 06:03:38.879: E/AndroidRuntime(938): FATAL EXCEPTION: main 10-02 06:03:38.879: E/AndroidRuntime(938): java.lang.IllegalArgumentException: contentView required: pkg=med.scheduler id=0 notification=Notification(vibrate=default,sound=content://settings/system/alarm_alert,defaults=0x2,flags=0x10) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.os.Parcel.readException(Parcel.java:1326) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.os.Parcel.readException(Parcel.java:1276) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.app.NotificationManager.notify(NotificationManager.java:111) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.app.NotificationManager.notify(NotificationManager.java:91) 10-02 06:03:38.879: E/AndroidRuntime(938): at med.scheduler.Enter_med$7.onClick(Enter_med.java:260) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.view.View.performClick(View.java:2485) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.view.View$PerformClick.run(View.java:9080) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.os.Handler.handleCallback(Handler.java:587) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.os.Handler.dispatchMessage(Handler.java:92) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.os.Looper.loop(Looper.java:123) 10-02 06:03:38.879: E/AndroidRuntime(938): at android.app.ActivityThread.main(ActivityThread.java:3683) 10-02 06:03:38.879: E/AndroidRuntime(938): at java.lang.reflect.Method.invokeNative(Native Method) 10-02 06:03:38.879: E/AndroidRuntime(938): at java.lang.reflect.Method.invoke(Method.java:507) 10-02 06:03:38.879: E/AndroidRuntime(938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-02 06:03:38.879: E/AndroidRuntime(938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-02 06:03:38.879: E/AndroidRuntime(938): at dalvik.system.NativeStart.main(Native Method) 10-02 06:03:43.855: I/Process(938): Sending signal. PID: 938 SIG: 9

     Intent alarmIntent = new Intent (Enter_med.this, MyAlarmService.class);

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pi = PendingIntent.getActivity(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification note = new Notification(R.drawable.ic_launcher, "Alarm", System.currentTimeMillis());
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
note.sound = alarmSound;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, note);

//alarm1
PendingIntent pendingAlarmIntent = PendingIntent.getService(Enter_med.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar AlarmCal = Calendar.getInstance();
AlarmCal.setTimeInMillis(System.currentTimeMillis());
AlarmCal.set(Calendar.HOUR_OF_DAY, pHour);
AlarmCal.set(Calendar.MINUTE, pMinute);
AlarmCal.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), pendingAlarmIntent);

最佳答案

在你的代码中

NotificationManager manager = (NotificationManager).getSystemService(Context.NOTIFICATION_SERVICE);

改成

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

关于android: NotificationManager 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12685167/

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