2012-10-02 73 views
1

請幫助我我是新來的android和我需要添加通知聲音到我的警報。我可以在哪裏放置通知聲音?這裏是我的通知,但有錯誤代碼「NotificationManager不能被解析爲一個變量」android:NotificationManager無法解析爲變量

應用力現在關閉我不知道爲什麼請大家幫忙,這裏是日誌貓

6月10日至二日: 03:38.785:W/dalvikvm(938):threadid = 1:線程退出與未捕獲的異常(組= 0x40015560) 10-02 06:03:38.879:E/AndroidRuntime(938):致命例外:主 10-02 06/03:38.879:E/AndroidRuntime(938):java.lang.IllegalArgumentException:contentView必需:pkg = med.scheduler id = 0 notification =通知(振動=默認,聲音=內容:/ /設置/系統/ alarm_alert,默認值= 0x2,標誌= 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):在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):在android.view.View.performClick(View.java:2485) 10-02 06:03:38.879:E/AndroidRun time(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):在android.os.Looper.loop(Looper.java:123) 10-02 06:03:38.879:E/AndroidRuntime(938):at android.app.ActivityThread.main(ActivityThread.java:3683)在Android中的E/AndroidRuntime(938):在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):發送信號。 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); 
+0

你可以嘗試清理一次你的項目嗎? – midhunhk

+0

我試過它總是強制關閉 –

+0

問題是你試圖啓動的活動,你需要設置其內容視圖 – Carnal

回答

1

您需要更改您的代碼:

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

只需卸下。之前的方法getSystemService()

自從您使用了以來,編譯器正在尋找名爲「NotificationManager」的變量。它旁邊的操作員。

1

在你的代碼

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

將其更改爲

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

謝謝。工作,但應用程序部隊關閉我不知道爲什麼這裏是logcat –

+0

你會通過編輯你的問題發佈logcat? – 2012-10-02 06:12:21

+0

我剛剛發佈了它 –

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); 
0

你忘了這個隊友:

note.setLatestEventInfo(this, "contentTitle", "contentText", pi); 
+0

我可以放在哪裏?對不起,只是一個初學者 –

+0

把它打電話之前通知(0,注意); – Carnal

+0

這個pendingIntent會告訴哪個活動開始 – Carnal