gpt4 book ai didi

android - android 中的通知不起作用

转载 作者:行者123 更新时间:2023-11-30 03:34:12 26 4
gpt4 key购买 nike

我正在我的应用程序中制作通知栏,这是我的代码:

主要 Activity :

package com.example.notificationservicedemo;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener {

EditText editMsg;
DatePicker datePicker;
TimePicker timePicker;
Button btnSetNotification;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

editMsg = (EditText) findViewById(R.id.editText1);
datePicker = (DatePicker) findViewById(R.id.datePicker1);
timePicker = (TimePicker) findViewById(R.id.timePicker1);
btnSetNotification = (Button) findViewById(R.id.buttonSetNotification);

btnSetNotification.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View view) {
Intent intent = new Intent();
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.buttonSetNotification:
String message=editMsg.getText().toString();
Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());

calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentHour());


AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


intent.setClass(this, MyNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
//startService(intent);

break;


}
}

}

我的通知服务:

package com.example.notificationservicedemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyNotificationService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "OnStart()", Toast.LENGTH_SHORT).show();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent= new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification = new Notification(R.drawable.ic_launcher, "Bla bla bla", System.currentTimeMillis());

String contentTitle="Title";
String contentText="This is your message";
notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
notificationManager.notify(123, notification);
}



}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationservicedemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

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

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

<service android:name="com.example.notificationservicedemo.MyNotificationService"></service>
</application>

</manifest>

问题是,当我选择必须出现通知的时间时,我按下按钮进行保存,然后通知立即出现,它忽略了我的 timePicker 和 datePicker。问题是什么?

最佳答案

这是我在 android 上启动通知的解决方案:

public class MyNotification {
private NotificationManager nm;
private Notification nf;
private int notification_id;

public MyNotification(Context context, Class<?> cls, int icon, CharSequence tickerText, CharSequence title, CharSequence text, int notification_id) {
nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancelAll();

Intent activity = new Intent(context, cls);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, activity, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent); //-> Activity open after click
builder.setAutoCancel(true);
builder.setSmallIcon(icon);
builder.setWhen(System.currentTimeMillis());
builder.setTicker(tickerText);
builder.setContentTitle(title);
builder.setContentText(text);

//builder.setSound(Uri.parse("android.resource://"+this.getPackageName()+R.raw.good)); //-> Song
//builder.setContent(new RemoteViews(getPackageName(), R.layout.note)); //-> Layout
//builder.setVibrate(new long[]{0, 100, 25, 100}); // -> Vibration

Notification nf = builder.getNotification();
this.nf = nf;
this.notification_id = notification_id;
}

public void show() {
this.nm.notify(notification_id, nf);
}
}

希望对你有所帮助!

关于android - android 中的通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16903235/

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