gpt4 book ai didi

java - 如何获得按钮点击通知?

转载 作者:行者123 更新时间:2023-11-29 19:23:41 25 4
gpt4 key购买 nike

我想在 fragment 中的 Button Click 事件上得到通知。我尝试了很多,它没有给我任何错误,但没有显示通知。

ma​​in.java

public class OneTimeTask extends Fragment implements View.OnClickListener {
public OneTimeTask() {
}

;

String addtask, adddetail, adddate;
static final int TIME_DIALOG_ID = 999;
View view;
EditText taskname, taskdetail, taskdate;
Button taskadd;
ProgressDialog mProgressDialog;
SessionManager session;
JSONObject jsonobject;
private DatePickerDialog fromDatePickerDialog;
private DatePickerDialog toDatePickerDialog;
private SimpleDateFormat dateFormatter;
String uid;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_daily__task, container, false);
getActivity().setTitle("Task");
taskname = (EditText) view.findViewById(R.id.taskname);
taskdetail = (EditText) view.findViewById(R.id.taskdetail);
taskdate = (EditText) view.findViewById(R.id.taskdate);
taskadd = (Button) view.findViewById(R.id.taskaddtask);

GetCurrentGPSLocation gps = new GetCurrentGPSLocation(getActivity());
if (gps.canGetLocation()) {

// \n is for new line

}else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
setDateTimeField();



return view;
}
private void setDateTimeField() {
taskadd.setOnClickListener(this);

Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
taskadd.setText(dateFormatter.format(newDate.getTime()));
}

},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

}

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

public void onClick(View view) {
if(view == taskadd) {
fromDatePickerDialog.show();
}
taskadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addtask=taskname.getText().toString();
adddetail=taskdetail.getText().toString();
adddate= taskdate.getText().toString();
InsertData();
Notification();

}
});
}

public void Notification() {
// Set Notification Title
String strtitle = getString(R.string.notificationtitle);
// Set Notification Text
String strtext = getString(R.string.notificationtext);

// Open NotificationView Class on Notification Click
/*
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
TabFragment mFrag = new TabFragment();
t.replace(com.Weal.sachin.omcom.R.id.framelayout, mFrag);
t.commit();
*/
Intent intent = new Intent(getActivity(), NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);

intent.putExtra("text", strtext);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(getContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

//Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
// Set Icon
.setSmallIcon(R.drawable.icon)
// Set Ticker Message
.setTicker(getString(R.string.notificationticker))
// Set Title
.setContentTitle(getString(R.string.notificationtitle))
// Set Text
.setContentText(getString(R.string.notificationtext))
// Add an Action Button below Notification
.addAction(R.drawable.ic_audiotrack, "Action Button", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);

// Create Notification Manager

// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getContext().getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());

}

这是我的 noficationview.class

        package com.Weal.sachin.omcom;

/**
* Created by sachin on 1/23/2017.
*/

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class NotificationView extends Activity {
String title;
String text;
TextView txttitle;
TextView txttext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationview);

// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Dismiss Notification
notificationmanager.cancel(0);

// Retrive the data from MainActivity.java
Intent i = getIntent();

title = i.getStringExtra("title");
text = i.getStringExtra("text");

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

// Set the data into TextView
txttitle.setText(title);
txttext.setText(text);
}
}

我试过这种方式,但这段代码没有在按钮点击时显示通知...

最佳答案

可能是因为你想添加

taskadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addtask=taskname.getText().toString();
adddetail=taskdetail.getText().toString();
adddate= taskdate.getText().toString();
InsertData();
Notification();

}
});

在 fragment 的 onClick() 内部。由于您已经在 onClick() 中,因此您将不需要 taskadd.setOnClickListener()。而是用这个替换你的 onClick() 方法。

if(view == taskadd) {
fromDatePickerDialog.show();
}
addtask=taskname.getText().toString();
adddetail=taskdetail.getText().toString();
adddate= taskdate.getText().toString();
InsertData();
Notification();

关于java - 如何获得按钮点击通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804540/

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