我有代碼,它只是不起作用,所以我請任何人幫助我。關於這個具體問題的信息很少。爲什麼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從不彈出。
問題:
所以問題縮小到Android只是無視我的通知。問題是它不告訴我爲什麼-_-,所以我無法修復它。任何關於此事的專家都會在我的代碼中看到有問題來調用通知?它在模擬器上重要嗎?
如果我把它拿走,我該如何更改我的清單文件的''標籤? –
Andy
2012-07-07 02:05:03
實際上,您可以從代碼中移除'intent.setAction(..)'並從android清單中刪除''部分。 –
2012-07-07 02:07:14
我剛剛做到了。仍然不起作用。感謝您指出它。它是一個開始。 – Andy 2012-07-07 02:07:58