gpt4 book ai didi

android - 为什么 Android 会忽略通知的创建?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:49 26 4
gpt4 key购买 nike

我有代码,但它不起作用,所以我请任何人帮助我。关于这个具体问题的信息非常少。

主 Activity


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



}
}

list


<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>


这应该就是一切。我正在尝试按原样运行它,但它没有显示任何内容。我在模拟器上运行它才是真正重要的。

编辑:当我说它不起作用时,我的意思是没有弹出任何内容。它运行良好,但从未弹出通知。

问题: enter image description here

所以问题缩小到 Android 只是忽略了我的通知。问题是它没有告诉我为什么 -_- 所以我无法修复它。是否有任何有关此事的专家看到我的代码有问题以调用通知?它在模拟器上重要吗?

最佳答案

我遇到了同样的问题。如果在创建“new Notification()”时没有指定图标,则不会出现通知。

关于android - 为什么 Android 会忽略通知的创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com