gpt4 book ai didi

java - 在我的应用程序中显示特定通知消息时出现问题

转载 作者:行者123 更新时间:2023-11-30 02:22:16 24 4
gpt4 key购买 nike

你好 frineds 我想在我的应用程序屏幕中显示我的特定通知消息所以下面是我的代码

主.java

public class NewMain extends Activity  {
// This is a handle so that we can call methods on our service
// This is the date picker used to select the date for our notification
private DatePicker picker;

int notificationCount;
String mStringTimeNotification="";
GetSecrateKey mGetSecrateKey;
ProgressDialog mProgressDialog;
PostParseGet mPostParseGet;
String mStringNewPass="";
String mStringOldPass="";
EditText mEditTextTitle;
EditText mEditTextDesc;
LinearLayout mLinearLayoutDate;
TextView mTextViewDate;
Calendar mCalendar;
Button mTextViewSave;
AllMethods mAllMethods;
boolean isClick=false;
int day,month,year,fday,fmm,fyy;
EditText mEditTextDate;
ImageView mImageViewBack;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_layout);
mTextViewMyAccount=(TextView)findViewById(R.id.titel_user);
// Create a new service client and bind our activity to this service
mPostParseGet=new PostParseGet(NewMain.this);
mAllMethods=new AllMethods(NewMain.this);
mLinearLayoutDate=(LinearLayout)findViewById(R.id.ll_date);
mEditTextDesc=(EditText)findViewById(R.id.edt_desc);
mGetSecrateKey=new GetSecrateKey();
mEditTextTitle=(EditText)findViewById(R.id.edt_title);
mTextViewSave=(Button)findViewById(R.id.btn_save);
mEditTextDate=(EditText)findViewById(R.id.edt_degree);
mImageViewBack=(ImageView )findViewById(R.id.img_back);
mCalendar = Calendar.getInstance();
day = mCalendar.get(Calendar.DAY_OF_MONTH);
month = mCalendar.get(Calendar.MONTH);
year = mCalendar.get(Calendar.YEAR);

SharedPreferences mSharedPreferences = getSharedPreferences(LoginActivity.MY_PREF, 0);
mStringGetSecrateKey=mSharedPreferences.getString("secratekey", "");
mTextViewMyAccount.setText(MyeHealthApplication.mStringUsername);
fday=day;
fmm=month+1;
fyy=year;
mEditTextDate.setText(String.valueOf(day)+"/"+String.valueOf(month+1)+"/"+String.valueOf(year));





mTextViewSave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mEditTextTitle.getText().toString().trim().equalsIgnoreCase("")) {
mAllMethods.ShowDialog(NewMain.this, "Validation error", "Enter title", "OK");
}
else {
if (isClick==true) {
fday = picker.getDayOfMonth();
fmm = picker.getMonth();
fyy = picker.getYear();
isClick=false;
setReminder(fyy, fmm, fday, 0, 0);
Toast.makeText(NewMain.this, "Notification set for: "+ fday +"/"+ (fmm+1) +"/"+ fyy, Toast.LENGTH_SHORT).show();
}
else {

setReminder(year, month, day, 0, 0);
Toast.makeText(NewMain.this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}

}
}
});
mLinearLayoutDate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mDialogPin.setContentView(R.layout.dialog_rim_date);
picker=(DatePicker)mDialogPin.findViewById(R.id.datePicker1);
TextView mTextViewsave=(TextView)mDialogPin.findViewById(R.id.dialog_save);
mTextViewsave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
isClick=true;
mDialogPin.dismiss();
fday = picker.getDayOfMonth();
fmm = picker.getMonth();
fyy = picker.getYear();

mEditTextDate.setText(String.valueOf(fday) +"/"+ (String.valueOf(fmm+1)) +"/"+ String.valueOf(fyy));
}
});
mDialogPin.show();
}
});
}



public void setReminder( int year,int month,int day,int hour,int minite)
{


notificationCount = notificationCount+1;
String[] parts = mEditTextDate.getText().toString().trim().split("/");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
String part3 = parts[2];

System.out.println("part1 "+part1);
System.out.println("part2 "+part2);
System.out.println("part3 "+part3);


year=Integer.parseInt(part1);
month=Integer.parseInt(part2);
day=Integer.parseInt(part3);

DecimalFormat formatter = new DecimalFormat("00");
String aFormatted = formatter.format(month);
String aFormatted1 = formatter.format(day);
String aFormatted2 =formatter.format(hour);
String aFormatted3 =formatter.format(minite);
mStringTimeNotification=year+"-"+aFormatted+"-"+aFormatted1+" "+aFormatted2+"-"+aFormatted3;
System.out.println("Sett DAteTiem " +mStringTimeNotification);

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh-mm");
Date dt = null;
try {
dt = df.parse(mStringTimeNotification);
}
catch (ParseException e) {
e.printStackTrace();
}
long when = dt.getTime();
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(NewMain.this, ReminderAlarm.class);
notificationIntent.putExtra("Name",mEditTextTitle.getText().toString().trim());
notificationIntent.putExtra("Description",mEditTextDesc.getText().toString().trim()+" at "+mEditTextDate.getText().toString().trim());
notificationIntent.putExtra("NotifyCount",notificationCount );

PendingIntent pi = PendingIntent.getBroadcast(NewMain.this, notificationCount, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP,when, pi);


}


}

ReminderAlarm.java

  public class ReminderAlarm  extends BroadcastReceiver{
private NotificationManager mNotificationManager;
private Notification notification;

@Override
public void onReceive(Context context, Intent intent) {

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = intent.getStringExtra("Name");
CharSequence message = intent.getStringExtra("Description");
Intent notificationIntent1 = new Intent(context, NotificationView.class);
notificationIntent1.putExtra("Name", from);
notificationIntent1.putExtra("Description", message);
notificationIntent1.putExtra("NotifyCount", intent.getExtras().get("NotifyCount").toString());

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent1, 0);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setTextViewText(R.id.noti_first, from);
expandedView.setTextViewText(R.id.noti_second, message);

Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setContentTitle(from).setContentText(message).build();
notification.defaults |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(Integer.parseInt(intent.getExtras().get("NotifyCount").toString()), notification);
Toast.makeText(context, "New Notification Received", Toast.LENGTH_SHORT).show();

}
}

NotificationView.java

public class NotificationView extends Activity  {
String title;
String text;
TextView txttitle;
TextView txttext;
String noti;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationview);
Intent i = getIntent();

title = i.getStringExtra("Name");
text = i.getStringExtra("Description");
noti =i.getStringExtra("NotifyCount");
System.out.println("title "+title);
System.out.println("text "+text);
System.out.println("noti "+noti);
// Locate the TextView
txttitle = (TextView) findViewById(R.id.title);
txttext = (TextView) findViewById(R.id.text);

txttitle.setText("Title : "+title);
txttext.setText(Html.fromHtml("<b> Description :</b>"+" "+text) );

}


}

当我运行上面的代码时,第一个通知在我的 Notificationview.java 类中显示正确,但是当我发出第二个通知并单击它时,它将显示我发出的第一个通知,它不会更新通知显示在 notificationview.java 类中,我该如何解决它?

最佳答案

在您的 NotificationView 类中实现覆盖方法。

/**
* Method to decide which detail view should be called on the basis of
* notification when notification is clicked.
* @param intent
*/
public void onNewIntent(Intent intent) {

try {
Bundle extras = intent.getExtras();
if (extras != null) {
title = intent.getStringExtra("Name");
text = intent.getStringExtra("Description");
noti = intent.getStringExtra("NotifyCount");

// Locate the TextView
txttitle = (TextView) findViewById(R.id.title);
txttext = (TextView) findViewById(R.id.text);

txttitle.setText("Title : "+title);
txttext.setText(Html.fromHtml("<b> Description :</b>"+" "+text) );
}
} catch (Exception e) {
e.printStackTrace();
}
}

在您的 onCreate 方法中,调用 onNewIntent(getActivity().getIntent());

希望它有用。

关于java - 在我的应用程序中显示特定通知消息时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314047/

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