5

我有代碼,它只是不起作用,所以我請任何人幫助我。關於這個具體問題的信息很少。爲什麼Android會忽略創建通知?

MainActivity


public class MainActivity extends Activity { 
public final int PENDING_INTENT_ID = 8; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button clickity = (Button)findViewById(R.id.alarm_button); 
    clickity.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar now = Calendar.getInstance(); 
      now.add(Calendar.SECOND, 20); 

      //Create a new PendingIntent used by the Alarm when it activates 
      Intent intent = new Intent(v.getContext(), AlarmReciever.class); 
      intent.setAction("SOME_AWESOME_TRIGGER_WORD"); 
      intent.putExtra("info", "This String shows that the info is actually sent correctly"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(v.getContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT|Intent.FILL_IN_DATA); 

      //Then Create the alarm manager to send this pending intent and set the date to go off 
      AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent); 

     } 
    }); 

} 

AlarmReciever(認識我拼寫錯了,但是因爲多數民衆贊成它是如何,即時通訊與它去)


public class AlarmReciever extends BroadcastReceiver{ 

@Override 
public void onReceive(Context c, Intent arg1) { 

    //get a reference to NotificationManager 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns); 

    //Instantiate the notification 

    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 
    Notification.Builder builder = new Notification.Builder(c) 
           .setTicker(tickerText) 
           .setWhen(when) 
           .setContentTitle(arg1.getStringExtra("info")) 
           .setContentText("Success!!") 
           .setAutoCancel(true); 
    Notification notification = builder.getNotification(); 
    mNotificationManager.notify(0, notification);//note the first argument, the ID should be unique 



} 
} 

清單


<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name="com.testproject.AlarmReciever" 
     android:enabled="true" 
     android:exported="false" > 
     <intent-filter> 
    </receiver> 

</application> 


這應該是一切。我試圖按原樣運行它,但它沒有顯示任何東西。我在模擬器上運行它實際上很重要。

編輯:當我說它不工作,我的意思是沒有彈出。它運行良好,但Notification從不彈出。

問題: enter image description here

所以問題縮小到Android只是無視我的通知。問題是它不告訴我爲什麼-_-,所以我無法修復它。任何關於此事的專家都會在我的代碼中看到有問題來調用通知?它在模擬器上重要嗎?

回答

2
Intent intent = new Intent(v.getContext(), AlarmReciever.class); 
// intent.setAction("SOME_AWESOME_TRIGGER_WORD"); replace to: 
intent.setAction("com.testproject.SOME_AWESOME_TRIGGER_WORD"); 

它至少先看看

UPD:

long firstTime = SystemClock.elapsedRealtime(); 

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, /* INTERVAL IN MS */ 20 * 1000, /* PendingIntent */ intent); 
+0

如果我把它拿走,我該如何更改我的清單文件的''標籤? – Andy 2012-07-07 02:05:03

+0

實際上,您可以從代碼中移除'intent.setAction(..)'並從android清單中刪除''部分。 – 2012-07-07 02:07:14

+0

我剛剛做到了。仍然不起作用。感謝您指出它。它是一個開始。 – Andy 2012-07-07 02:07:58

8

好了,課上通知教訓。我得到錯誤的原因是因爲img必須添加,如果沒有,它不會顯示!除弗拉基米爾慷慨地指出的外,我所擁有的一切基本上都是正確的。在此發佈此信息以防其他人收到類似的問題,只是測試通知。

22

我遇到了同樣的問題。如果在創建「新通知()」時未指定圖標,則不會顯示通知。

+1

哈哈,是的。我希望文檔提到這一點。但是,嘿,試錯了。現在其他人知道:) – Andy 2012-11-03 22:47:43

0

這是因爲您沒有設置通知圖標。如果你設置了notificaiton圖標,它應該可以工作。日誌本身說,你不能發送沒有圖像的通知。

0

好像你忘了設置圖標..你需要ATLEAST設置的默認圖標..

試試這個..

Notification.Builder builder = new Notification.Builder(c) 
         .setTicker(tickerText) 
         .setWhen(when) 
         .setContentTitle(arg1.getStringExtra("info")) 
         .setContentText("Success!!") 
         .setAutoCancel(true) 
         .setSmallIcon(R.drawable.ic_launcher); //<--- this one