Below are my 2 classes. The ViewTask class calls the notification method in broadcastmanager clas passing a parameter. But the parameter is shown null inside the notification method. Pleas advice why the message I pass from here is not passed onto notification method.
package com.example.tasker;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
public class BroadcastManager extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
this.Notification(context, "Get the task from database");
}
public void Notification(Context context,String message) {
System.out.println("Message is "+message);
String strtitle = "To Do Notification";
Intent intent = new Intent(context, NotificationView.class);
intent.putExtra("title", strtitle);
intent.putExtra("text", message);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(message)
.setContentTitle("To Do App notification")
.setContentText(message)
.addAction(R.drawable.ic_launcher, "Action Button", pIntent)
.setContentIntent(pIntent)
.setAutoCancel(true);
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.notify(0, builder.build());
}
}
package com.example.tasker;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class ViewTask extends Activity {
protected TaskerDbHelper db;
List<task> list;
MyAdapter adapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
db = new TaskerDbHelper(this);
list = db.getAllTasks();
adapt = new MyAdapter(this, R.layout.list_inner_view, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
new GetCDates().execute("");
}
private class GetCDates extends AsyncTask<string,>
{
Task current;
String date,notifyDate;
int day,month,year;
@Override
protected String doInBackground(String... params) {
try {
Calendar c = Calendar.getInstance();
day = c.get(Calendar.DAY_OF_MONTH);
month=c.get(Calendar.MONTH);
year=c.get(Calendar.YEAR);
String cdate=day+"-"+month+"-"+year;
System.out.println("today is "+cdate);
loop:for(int j=0;j<list.size();j++)>
{
current = list.get(j);
date=current.getDate();
System.out.println("date is "+date);
if(date.equals(cdate))
{
notifyDate=date;
break loop;
}
}
String taskArray[]=db.getTaskForTheDay(notifyDate);
BroadcastManager b=new BroadcastManager();
for(int i=0;i<taskarray.length;i++)>
b.Notification(ViewTask.this, taskArray[i]);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return date;
}
}
public void addTaskNow(View v) {
EditText t = (EditText) findViewById(R.id.editText1);
EditText t2=(EditText)findViewById(R.id.editText2);
String s = t.getText().toString();
String s2 = t2.getText().toString();
if (s.equalsIgnoreCase("")) {
Toast.makeText(this, "enter the task description first!!",
Toast.LENGTH_LONG);
}
else {
if (s.equalsIgnoreCase("")) {
Toast.makeText(this, "enter the date!!",
Toast.LENGTH_LONG);
}
else{
Task task = new Task(s, 0,s2);
db.addTask(task);
Log.d("tasker", "data added");
t.setText("");
t2.setText("");
adapt.add(task);
adapt.notifyDataSetChanged();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_view_task, menu);
return true;
}
private class MyAdapter extends ArrayAdapter<task> {
Context context;
List<task> taskList = new ArrayList<task>();
int layoutResourceId;
public MyAdapter(Context context, int layoutResourceId,
List<task> objects) {
super(context, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.taskList = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox chk = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_inner_view,
parent, false);
chk = (CheckBox) convertView.findViewById(R.id.chkStatus);
convertView.setTag(chk);
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Task changeTask = (Task) cb.getTag();
changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
db.updateTask(changeTask);
Toast.makeText(
getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG)
.show();
}
});
} else {
chk = (CheckBox) convertView.getTag();
}
Task current = taskList.get(position);
chk.setText(current.getTaskName()+" - "+current.getDate());
chk.setChecked(current.getStatus() == 1 ? true : false);
chk.setTag(current);
Log.d("listener", String.valueOf(current.getId()));
return convertView;
}
}
}