我有代码,它只是不起作用,所以我请任何人帮助我。关于这个具体问题的信息非常少。
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>
这应该是一切。我试图按原样运行它并没有向我显示任何内容。我在模拟器上运行它实际上很重要。
编辑:当我说它不起作用时,我的意思是什么都没有弹出来。它运行良好,但通知永远不会弹出。问题:
因此问题缩小到Android而忽略了我的通知。问题是它没有告诉我为什么-_-所以我无法解决它。有关此问题的专家看到我的代码调用通知有问题吗?它在模拟器上是否重要?
答案 0 :(得分:22)
我遇到了同样的问题。如果在创建“新通知()”时未指定图标,则不会显示通知。
答案 1 :(得分:8)
嗯,在通知中吸取了教训。我收到错误的原因是因为必须添加img,如果没有,它将不会显示!除了弗拉基米尔慷慨地指出的东西之外,我所拥有的其他一切基本上都是正确的。其他人在测试通知时遇到了类似的问题。
答案 2 :(得分: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);
答案 3 :(得分:0)
这是因为您没有设置通知图标。如果您设置通知图标,它应该工作。日志本身表示您无法在没有图像的情况下发送通知。
答案 4 :(得分:0)
好像你忘了设置Icon ..你需要至少设置一个默认图标..
试试这个..
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