Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.


Java
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) {
		// TODO Auto-generated method stub
		this.Notification(context, "Get the task from database");
	}

	public void Notification(Context context,String message) {
		System.out.println("Message is "+message);

        // Set Notification Title
        String strtitle = "To Do Notification";
        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(context, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", message);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
 
        // Create Notification using NotificationCompat.Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context)
                // Set Icon
                .setSmallIcon(R.drawable.ic_launcher)
                // Set Ticker Message
                .setTicker(message)
                // Set Title
                .setContentTitle("To Do App notification")
                // Set Text
                .setContentText(message)
                // Add an Action Button below Notification
                .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // Dismiss Notification
                .setAutoCancel(true);
 
        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        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);
//		while(true)
		
		new GetCDates().execute("");
		
	}
	
	
	
	
	private class GetCDates extends AsyncTask<string,>
	{
		Task current;
		
		String date,notifyDate;
		int day,month,year;
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub   
	                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]);
//	            		PackageManager pm = ViewTask.this.getPackageManager();
//	            	      ComponentName componentName = new ComponentName(
//	            	              ViewTask.this, BroadcastManager.class);
//	            	      pm.setComponentEnabledSetting(componentName,
//	            	              PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
//	            	              PackageManager.DONT_KILL_APP);
//	            	      Toast.makeText(getApplicationContext(),
//	            	              "Broadcast Receiver Started", Toast.LENGTH_LONG)
//	            	              .show();
	                    Thread.sleep(2000);
	                } catch (InterruptedException e) {
	                    // TODO Auto-generated catch block
	                    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) {
		// Inflate the menu; this adds items to the action bar if it is present.
		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;
		}

		/**
		 * This method will DEFINe what the view inside the list view will
		 * finally look like Here we are going to code that the checkbox state
		 * is the status of task and check box text is the task name
		 */
		@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;
		}

	}

}
Posted
Updated 24-Apr-14 22:24pm
v2

Java
public void Notification(Context context,String message) {


Notification?? Please think about that.

You should put some logging in there to see what your db-request is delivering.

http://developer.android.com/reference/android/util/Log.html[^]

I bet that already gives answers.

Then you can go for some debugging to see where it fails.
http://developer.android.com/tools/debugging/index.html[^]

You can set a break point on the NullpointerException - if you get one.
 
Share this answer
 
The problem is the arrayList I have added here.In the database creation class I specified it as size 1000. Therefore the for loop here calling notification is giving error.Changing it to a List worked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900