我想要片段中的Button Click事件通知。我尝试了很多,它没有给我任何错误,但是没有显示通知。
main.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);
}
}
我尝试过这种方式,并且此代码未在Button Click上显示通知。
可能是因为您要添加
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();
}
});
在片段的onClick()中。由于您已经在onClick()中,因此不需要taskadd.setOnClickListener()。而是用此替换onClick()方法。
if(view == taskadd) {
fromDatePickerDialog.show();
}
addtask=taskname.getText().toString();
adddetail=taskdetail.getText().toString();
adddate= taskdate.getText().toString();
InsertData();
Notification();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句