1

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

应用程序力量现在关闭我不知道为什么请帮忙,这是日志猫

3683) 10-02 06:03:38.879: E/AndroidRuntime(938): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-02 06:03:38.879: E/AndroidRuntime(938): 在 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): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-02 06:03:38.879: E/AndroidRuntime( 938): 在 dalvik.system.NativeStart.main(Native Method) 10-02 06:03:43.855: I/Process(938): 发送信号。PID:938 SIG:9 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): 发送信号。PID:938 SIG:9 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): 发送信号。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);
4

5 回答 5

1

您需要将代码更改为:

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

只需删除 . 在getSystemService()方法之前。

编译器正在寻找一个名为“NotificationManager”的变量,因为您使用了 . 旁边的接线员。

于 2012-10-02T05:55:43.730 回答
1

在您的代码中

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

将其更改为

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
于 2012-10-02T05:57:26.897 回答
0

如果你愿意,你可以这样做

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World! " + android_id;
        Intent notificationIntent = new Intent(this,
                ExpMapLatLongActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);

        mNotificationManager.notify(HELLO_ID, notification);
于 2012-10-02T06:01:03.447 回答
0

你忘记了这个伙伴:

note.setLatestEventInfo(this, "contentTitle", "contentText", pi);
于 2012-10-02T06:28:47.800 回答
0

You can also use other solution by using NotificationManagerCompat. try this code

NotificationManagerCompat mNotifyMgr =
    NotificationManagerCompat.from(mCtx);

if (mNotifyMgr != null) {
    mNotifyMgr.notify(1, mBuilder.build());
}

where mCtx is context or you can getApplicationContext()

于 2021-03-18T03:59:02.273 回答