E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb96ae9c0 getting this error - android

i am doing a notification view project. notification does appear on notification bar, when i click on notification bar it will go on next activity to display the same info that notification what has. but got an error this...
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb96ae9c0
my code is
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button view_notifi,btn_time,btn_date;
int notificationID,getHour,getMin,getDay,getMonth,getYear;
final int dialogTime=0,dialogDate=1;
Notification notification;
EditText edt_heading,edt_details,edt_details2;
String head,details,details2;
String Time,setDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_heading=(EditText)findViewById(R.id.editText_Heading);
edt_details=(EditText)findViewById(R.id.editText_Details);
edt_details2=(EditText)findViewById(R.id.editText_details2);
notificationID=1;
view_notifi=(Button)findViewById(R.id.button_viewN);
btn_time=(Button)findViewById(R.id.button_time);
btn_date=(Button)findViewById(R.id.button_date);
view_notifi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
head=edt_heading.getText().toString();
details=edt_details.getText().toString();
details2=edt_details2.getText().toString();
displayNotification(head,details,details2);
edt_heading.getText().clear();
edt_details.getText().clear();
edt_details2.getText().clear();
}
});
}
public void getTimeInfo(View view){
showDialog(dialogTime);
}
public void getDateInfo(View view){
showDialog(dialogDate);
}
protected Dialog onCreateDialog(int id){
switch (id){
case dialogTime:
return new TimePickerDialog(this,mTimeListener,getHour,getMin,false);
case dialogDate:
DatePickerDialog dialog = new DatePickerDialog(this, mDateListener, getYear, getMonth, getDay);
DatePicker datePicker = dialog.getDatePicker();
Calendar calendar = Calendar.getInstance();
datePicker.setMinDate(calendar.getTimeInMillis());
//dialog.getDatePicker().setMinDate(new Date().getTime());
return dialog;
//return new DatePickerDialog(this,mDateListener,getYear,getMonth,getDay);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeListener=new TimePickerDialog.OnTimeSetListener(){
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
getHour=hourOfDay; getMin=minute;
Time=String.valueOf(" "+getHour+":"+getMin);
Toast.makeText(getBaseContext(),"Time: "+Time,Toast.LENGTH_SHORT).show();
}
};
private DatePickerDialog.OnDateSetListener mDateListener=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
getYear=year; getMonth=monthOfYear+1;getDay=dayOfMonth;
//getMonth=+1;
setDate=String.valueOf(" "+getDay+"/"+getMonth+"/"+getYear);
Toast.makeText(getBaseContext(),"Date: "+setDate,Toast.LENGTH_SHORT).show();
}
};
public void displayNotification(String str1,String str2,String str3){
Intent intent = new Intent("notify_filter");
Bundle bundle = new Bundle();
bundle.putString("arg1", str1);
bundle.putString("arg2", str2);
bundle.putString("arg3", str3);
bundle.putString("arg4", Time);
bundle.putString("arg5", setDate);
intent.putExtras(bundle);
intent.putExtra("notificationID",notificationID);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(true);
builder.setTicker("New Alert...");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(str1);
builder.setContentText(str2);
builder.setSubText("Time " + Time + " Date:" + setDate); //API level 16
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setNumber(100);
builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = builder.getNotification();
notification.vibrate = new long[]{100, 250, 100, 400};
notificationManager.notify(notificationID, notification);
}
}
ViewNotification.java
public class ViewNotification extends AppCompatActivity {
TextView head,detail1,detail2,time,date;
Bundle bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_notification);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(getIntent().getExtras().getInt("notificationID"));
head=(TextView)findViewById(R.id.textView_head);
detail1=(TextView)findViewById(R.id.textView_det1);
detail2=(TextView)findViewById(R.id.textView_det2);
time=(TextView)findViewById(R.id.textView_time);
date=(TextView)findViewById(R.id.textView_date);
bundle = getIntent().getExtras();
String view1= bundle.getString("arg1");
String view2= bundle.getString("arg2");
String view3= bundle.getString("arg3");
//String view4=bundle.getString("arg4");
//String view5=bundle.getString("arg5");
head.setText(view1);
detail1.setText(view2);
detail2.setText(view3);
time.setText(bundle.getChar("arg4"));
date.setText(bundle.getChar("arg5"));
}
}

This happens when you try to set value of TextView to an integer.
When you assign a value of String to a TextView this error disappears.
For example
I have create a Text View in xml file. I am trying to set the value of that particular text field from java code. I am setting the int value to Text View but I am receiving an error getSlotFromBufferLocked: unknown buffer: 0xab7519c0 but after changing value from int to String and setting TextView from that String error disappears and app is working fine.

Related

open an activity from a fragment once the data is stored in database

I need to open the add new tasks activity once the data is saved in the database inside a fragment. But when I call the intent function it'snot working and the app is getting closed unfortunately. I couldn't find what the error is exactly. But once I comment the Intent function the add button is working properly. Please solve this for me.
package com.example.prami.prami;
public class AddNewAssignmentFragment extends Fragment{
//code elements
View myView;
EditText txtAddAssignmentName;
Spinner ddTasksNo;
EditText txtStartDate;
EditText txtEndDate;
// DatePickerDialog datePickerDialog;
Button btnAddAssignmentToDB;
// Button btnGotoAddTasksPage;
SQLiteDatabase assignDB;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.add_new_assignment, container, false);
txtAddAssignmentName = (EditText) myView.findViewById(R.id.txtAssignName);
txtStartDate = (EditText)myView.findViewById(R.id.txtHandoverDate);
txtEndDate = (EditText)myView.findViewById(R.id.txtSubmissionDate);
btnAddAssignmentToDB = (Button) myView.findViewById(R.id.btnAddAssignment);
// btnGotoAddTasksPage = (Button)myView.findViewById(R.id.btnAddTask);
ddTasksNo=(Spinner)myView.findViewById(R.id.spNoOfTasks);
String taskNoArray[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; //Java Array
ArrayAdapter<String> adbTaskNo = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, taskNoArray);
adbTaskNo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ddTasksNo.setAdapter(adbTaskNo);
txtStartDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment picker = new DatePickerDialogThemeStart();
picker.show(getFragmentManager(), "handoverdatePicker");
}
});
txtEndDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment picker = new DatePickerDialogThemeEnd();
picker.show(getFragmentManager(), "submissiondatePicker");
}
});
btnAddAssignmentToDB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CreateDB();
AddAssignRecordToDB();
}
});
// btnGotoAddTasksPage.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// GoToAddTasksPage();
// }
// });
return myView;
}
public static class DatePickerDialogThemeStart extends DialogFragment implements DatePickerDialog.OnDateSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int hyear = calendar.get(Calendar.YEAR);
int hmonth = calendar.get(Calendar.MONTH);
int hday = calendar.get(Calendar.DAY_OF_MONTH);
#SuppressWarnings("ResourceType")
DatePickerDialog stdatepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,hday,hmonth,hyear);
stdatepickerdialog.setTitle("Select Handover Date");
calendar.add(Calendar.DATE,0);
stdatepickerdialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
return stdatepickerdialog;
}
public void onDateSet(DatePicker view, int styear, int stmonth, int stday) {
if (view.isShown()) {
EditText txtStDate = (EditText) getActivity().findViewById(R.id.txtHandoverDate);
String handoverdate = String.valueOf(styear) + "/" + String.valueOf(stmonth + 1) + "/" + String.valueOf(stday);
txtStDate.setText(String.format(handoverdate,getActivity()));
}
}
}
public static class DatePickerDialogThemeEnd extends DialogFragment implements DatePickerDialog.OnDateSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int subyear = calendar.get(Calendar.YEAR);
int submonth = calendar.get(Calendar.MONTH);
int subday = calendar.get(Calendar.DAY_OF_MONTH);
#SuppressWarnings("ResourceType")
DatePickerDialog endatepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,subday,submonth,subyear);
endatepickerdialog.setTitle("Select Submission Date");
calendar.add(Calendar.DATE,0);
endatepickerdialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
return endatepickerdialog;
}
public void onDateSet(DatePicker view, int endyear, int endmonth, int endday) {
if (view.isShown()) {
EditText txtEnDate = (EditText) getActivity().findViewById(R.id.txtSubmissionDate);
String submissiondate = String.valueOf(endyear) + "/" + String.valueOf(endmonth + 1) + "/" + String.valueOf(endday);
txtEnDate.setText(String.format(submissiondate,getActivity()));
}
}
}
public void CreateDB()
{
assignDB = getActivity().openOrCreateDatabase("timeFighter", Context.MODE_PRIVATE, null);
assignDB.execSQL("Create TABLE IF NOT EXISTS myAssignInfo(id INTEGER PRIMARY KEY, assignName VARCHAR(50), " +
"numOfTasks INTEGER, stDate VARCHAR(10), enDate VARCHAR(10))");
}
protected void AddAssignRecordToDB()
{
try {
String assignName = txtAddAssignmentName.getText().toString().trim();
if (assignName.matches("")) {
Toast.makeText(getActivity().getApplicationContext(),"You did not enter an assignment name", Toast.LENGTH_SHORT).show();
return;
}
int assignTaskNo = Integer.parseInt(ddTasksNo.getSelectedItem().toString().trim());
if(ddTasksNo == null && ddTasksNo.getSelectedItem() == null ) {
assignTaskNo = (Integer) ddTasksNo.getSelectedItem();
}
String assignStartDate = txtStartDate.getText().toString().trim();
if (assignStartDate.matches("")) {
Toast.makeText(getActivity().getApplicationContext(),"You did not enter a handover date", Toast.LENGTH_SHORT).show();
return;
}
String assignEndDate = txtEndDate.getText().toString().trim();
if (assignEndDate.matches("")) {
Toast.makeText(getActivity().getApplicationContext(),"You did not enter a submission date", Toast.LENGTH_SHORT).show();
return;
}
String query = "Insert into myAssignInfo (assignName, numOfTasks, stDate," +
" enDate) Values('"+ assignName +"', "+ assignTaskNo +", '"+ assignStartDate +"', '"+ assignEndDate +"')";
assignDB.execSQL(query); //executing query
Toast.makeText(getActivity().getApplicationContext(),"Assignment added successfully", Toast.LENGTH_SHORT).show();
GoToAddTasksPage();
}
catch (Exception ex)
{
Toast.makeText(getActivity().getApplicationContext(),ex.getMessage(), Toast.LENGTH_SHORT).show();
//("ex.printStackTrace")
}
}
protected void GoToAddTasksPage()
{
Intent goToTasks = new Intent(getActivity(), AddTasks.class);
startActivity(goToTasks);
}
}

working with android alarmmanager

I have a reminder kind of app where users will set their reminders.
Most of the tutorials and example that i found were of alarm manager in which alarm is set up after specific time or at pre defined time. but how can i set it to user entered data and time. Also, there will be more than one alarms set up by the user.
All these are stored in a database and then populated in a lsitview.
This is a fragment in which user will be setting date and time: (texts "dte" and "tme" displays date and time that user has selected) and then when user clicks save button, i want to set alarm at the time that is selected by user.
Calendar calender = Calendar.getInstance();
TextView dte,tme, doit;
ImageButton cal;
EditText enteredTask;
Button save;
String taskentered, dateentered, timeentered;
tasks_Database_Operations tasksDatabaseOperations;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_task_fragment, container, false);
dte = (TextView) view.findViewById(R.id.datetv);
tme = (TextView) view.findViewById(R.id.timetv);
Log.d("LifeCycle HirakDebug", "aTF View Created");
return view;
}
#Override
public void onResume() {
super.onResume();
Log.d("LifeCycle HirakDebug", "aTF Resume");
enteredTask = (EditText) getActivity().findViewById(R.id.user_entered_task);
doit = (TextView) getActivity().findViewById(R.id.title);
cal = (ImageButton) getActivity().findViewById(R.id.select_date);
save = (Button) getActivity().findViewById(R.id.save_tsk);
save.setOnClickListener(this);
cal.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == cal) {
FragmentManager fm = getFragmentManager();
date_time_picker dTP = new date_time_picker();
dTP.setTargetFragment(this, 1);
dTP.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myCustomDialog);
dTP.show(fm, "dTP");
Log.d("HirakDebug", "add_task_frag setDate called");
} else if (view == save) {
saveData();
Log.d("HirakDebug", "add_task_frag saveData called");
}
}
private void saveData() {
String iidd = null;
taskentered = enteredTask.getText().toString();
dateentered = dte.getText().toString();
timeentered = tme.getText().toString();
tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
long id = tasksDatabaseOperations.insertData(iidd, taskentered, dateentered, timeentered);
if (id < 0) {
Log.e("HirakDebug", "add_task_frag failed insertData operation");
} else {
Log.d("HirakDebug", "Data sent to be inserted");
}
tasksDatabaseOperations.sqLiteDatabase.close();
goBackToTaskListFragment();
}
private void goBackToTaskListFragment() {
tasksListFrag tLF = new tasksListFrag();
add_task_frag aTF = new add_task_frag();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down);
ft.remove(this);
ft.replace(R.id.dynamic_content, tLF, "tLF");
ft.commit();
Log.d("HirakDebug", "add_task_frag went back to taskListFrag");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1){
String date = data.getStringExtra("date");
String time = data.getStringExtra("time");
dte.setText(date);
tme.setText(time);
}
}
Perhaps Repeating Alarm the one you need to use.
Please click on the link below...
Set a Repeating Alarm
https://developer.android.com/training/scheduling/alarms.html
Code example form the link above:
Wake up the device to fire the alarm at precisely 8:30 a.m., and every 20 minutes thereafter:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);

my multiple alarms does not work | android

hi i have a problem with my code in the multiple alarm . i typed my code to be work on a multiple alarms every alarm take 10 seconds to be triggered REPEATEDLY. my problem that the alarm work with just one value but although i have put a different requestCode, but it cannot bet work or i don't know how i call every one separately .
the problem is i'v try to do this--->
alarm1 with a value and a request code 0 (for example) for 10 seconds
alarm2 with a different value and a request code 1 (for example) for 10 seconds
the code do this---->
desplay an just alarm2 that i selected lately
static int HELLO_ID = 1;
boolean flag = false;
int CountMultipleAlarm = 0;
EditText edt,edt2;
Button btn;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String edittext= edt.getText().toString();
Pattern pat= Pattern.compile("[0-9]");
// Pattern pat= Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = pat.matcher(edittext);
//*************Timer Start *************************8
//11000 = 10 seconds(11000*6*60 == 1hour)
int count = 11000;
timer = new CountDownTimer(count, 1000)
{public void onTick(long millisUntilFinished)
{
long scnds=0;
scnds=(millisUntilFinished/1000);
}
public void onFinish()
{//Alaram cooooode **********************************************************
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
AlarmManager am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
//the title and the description of the notification
Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
alarmintent.putExtra("title",titlePills + "value");
alarmintent.putExtra("note","value");
//HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//Alarm coooooode end **************************************
intentArray.add(sender);
timer.start();
}
}.start();
//*************Timer Ends *************************8
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}import
and this is the alarm_Receiver class
public class Alarm_Receiver extends BroadcastReceiver {
String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notification1,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, Alarm_Receiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
//
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our
//notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
};
You are overwriting the old one because the request code that you use in getBroadcast() is always zero.
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), i++, alarmintent,
PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
The value of i is always zero, because you always set it to zero before calling getBroadcast().

How can I use multiple Alarms at once - Android

Hi, I have a problem with my code.I typed my code to be work on a multiple alarms every alarm take 10 seconds to be triggered REPEATEDLY.My problem is that the alarm works with just one value but although I've put a different requestCode,but it doesn't work or I don't know how I can call every one separately .
I've tried to do this:
alarm1 with a value and a request code 0 (for example) for 10 seconds
alarm2 with a different value and a request code 1 (for example) for
10 seconds
The code does:
Display Alarm2 that I selected last.
static int HELLO_ID = 1;
boolean flag = false;
int CountMultipleAlarm = 0;
EditText edt,edt2;
Button btn;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String edittext= edt.getText().toString();
Pattern pat= Pattern.compile("[0-9]");
// Pattern pat= Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = pat.matcher(edittext);
//*************Timer Start ******************
//11000 = 10 seconds(11000*6*60 == 1hour)
int count = 11000;
timer = new CountDownTimer(count, 1000)
{public void onTick(long millisUntilFinished)
{
long scnds=0;
scnds=(millisUntilFinished/1000);
}
public void onFinish()
//****** Alarm Code ********
Calendar cal = Calendar.getInstance();
// for using this you need to import java.util.Calendar;
AlarmManager am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
//the title and the description of the notification
Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
alarmintent.putExtra("title",titlePills + "value");
alarmintent.putExtra("note","value");
//HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//Alarm coooooode end **************************************
intentArray.add(sender);
timer.start();
}
}
//************* Timer Ends *************
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}import
//Edit from Paramone: What comes after " }import " ???
alarm_Receiver class
public class Alarm_Receiver extends BroadcastReceiver {
String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notification1,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, Alarm_Receiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
};

Calculation of current time in android

suppose I click start button and close the application but Notification is running. After five miutes I open notification then second activity opens and settext with 5 min.Again i close appliction.Then again I click on start button.suppose after 3 min i check notification it should open with 3 min not 8.but in my application it show 8 min
1:Main ACtivity where notification is start
public class MainActivity extends Activity {
final Context context=this;
private EditText timerValue;
private static final int NOTIFY_ME_ID=1337;
Intent MyIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerValue = (EditText) findViewById(R.id.timerValue111);
startButton = (Button) findViewById(R.id.startButton);
x=timerValue.getText().toString();
nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
startButton.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View view) {
Long s1=(long) 0.0;
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
//Toast.makeText(MainActivity.this,timeStamp,2000).show();
Date interestingDate = new Date();
s1= interestingDate.getTime();
int seconds = (int) (s1/ 1000) % 60 ;
int minutes = (int) ((s1 /(1000*60)) % 60);
int hours = (int) ((s1 /(1000*60*60)) % 24);
Toast.makeText(MainActivity.this,"Time:"+hours+":"+minutes+":"+seconds,2000).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() );
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears
String MyNotificationTitle = "Medicine!";
String MyNotificationText = "Don't forget to take your medicine!";
MyIntent = new Intent(MainActivity.this,SecondActivity.class);
MyIntent.putExtra("s1", (long)s1);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,0);
// x=timerValue.getText().toString();
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
});
}
}
2:Second Activity where total time is calculated
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
long p=(long) 0.0;
long s=(long) 0.0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView text1=(TextView)findViewById(R.id.text1);
Bundle extras = getIntent().getExtras();
Date interestingDate = new Date();
s= interestingDate.getTime();
long x1=extras.getLong("s1");
p=s-x1;
int minutes = (int) ((p /(1000*60)) % 60);
text1.setText(""+minutes);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
That is happening maybe because you do not close the application, so the application goes in onPause state, and your second activity is not calling the onDestroy => when you enter again in the second activty, is called the onResume method, where you do not set the TextView text

Categories

Resources