Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an activity called PerryRoad.java as following.

Java
package com.example.streetparkingsystem;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class PerryRoad extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.perryroad);

    Button b = (Button)findViewById(R.id.b1);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

 String uri = "https://maps.google.com/maps?f=d&daddr=Perry road,Bandra West,Mumbai";
 Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
 intent.setClassName("com.google.android.apps.maps",
     "com.google.android.maps.MapsActivity");
       startActivity(intent);
          }
    });

    Button m = (Button)findViewById(R.id.b2);
    m.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    Intent lomap = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(lomap);
        }
    });



}
  }

This is basically an app for parking vehicles. The amount is fixed 100 Rs. to park at this place.As soon as user clicks get map button (perryroad.xml) app redirects user to MainActivity.java which is as follows.


Java
package com.example.streetsystemparking;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;




import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
	
	Button refresh;
	Button bv1;
	Button bv2;
	Button bv3;
	Button bv4;
	Button bv5;
	Button bv6;
	Button bv7;
	Button bv8;
	String uid;
	String status;
	InputStream is=null;
	String result=null;
	String line=null;
	HttpResponse response;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		refresh = (Button)findViewById(R.id.refresh);
		bv1 = (Button)findViewById(R.id.button1);
		bv2 = (Button)findViewById(R.id.button2);
		bv3 = (Button)findViewById(R.id.button3);
		bv4 = (Button)findViewById(R.id.button4);
		bv5 = (Button)findViewById(R.id.button5);
		bv6 = (Button)findViewById(R.id.button6);
		bv7 = (Button)findViewById(R.id.button7);
		bv8 = (Button)findViewById(R.id.button8);
        bv1.setOnClickListener(this);
		bv2.setOnClickListener(this);
		bv3.setOnClickListener(this);
		bv4.setOnClickListener(this);
		bv5.setOnClickListener(this);
		bv6.setOnClickListener(this);
		bv7.setOnClickListener(this);
		bv8.setOnClickListener(this);

		 refresh.setOnClickListener(new View.OnClickListener(){
	            public void onClick(View arg0){

	            	Intent intent = getIntent();
	                finish();
	                startActivity(intent);

	            }

	        });
		
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					uid="1";
					select();
					uid="2";
					select();	
					uid="3";
					select();
					uid="4";
					select();
					uid="5";
					select();	
					uid="6";
					select();
					uid="7";
					select();
					uid="8";
					select();
			
				}
			}).start();


	}
	
    public void onClick(View v) {
       switch(v.getId()){
           case R.id.button1:
        	 
          	if (bv1.getText().toString().equals("R1"))
          	{
          		showAlert1();
          		
          	}
          	else
          	{
          		Toast.makeText(MainActivity.this,"You clicked on block 1",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment1Activity.class),1);
          	}
   				   				
				
		   break;
		    
            case R.id.button2:
            	
            	if (bv2.getText().toString().equals("R2"))
            	{
            		showAlert2();
            	}
            	else
            	{
            		
                 Toast.makeText(MainActivity.this,"You clicked on block 2",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment2Activity.class),2);
            	}
    				
		    break;
            
            case R.id.button3:
            	 
            	if (bv3.getText().toString().equals("R3"))
            	{
            		showAlert3();
            	}
            	else{
                 Toast.makeText(MainActivity.this,"You clicked on block 3",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment3Activity.class),3);
            	}
    				
            	
            break;
            
            case R.id.button4:
            	if (bv4.getText().toString().equals("R4"))
              	{
              		showAlert4();
              		
              	}
              	else
              	{
            	 Toast.makeText(MainActivity.this,"You clicked on block 4",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment4Activity.class),4);
              	}
           	break;
           	
            case R.id.button5:
            	if (bv5.getText().toString().equals("R5"))
              	{
              		showAlert5();
              		
              	}
              	else
              	{
            	 Toast.makeText(MainActivity.this,"You clicked on block 5",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment5Activity.class),5);
              	}
            break;
            
            case R.id.button6:
            	if (bv6.getText().toString().equals("R6"))
              	{
              		showAlert6();
              		
              	}
              	else
              	{
              		Toast.makeText(MainActivity.this,"You clicked on block 6",Toast.LENGTH_LONG).show();
				    startActivityForResult(new Intent(MainActivity.this,Payment6Activity.class),6);
              	}
            break;
            
            case R.id.button7:
            	if (bv7.getText().toString().equals("R7"))
              	{
              		showAlert7();
              		
              	}
              	else
              	{
            	 Toast.makeText(MainActivity.this,"You clicked on block 7",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment7Activity.class),7);
              	}
            break;
            
            case R.id.button8:
            	if (bv8.getText().toString().equals("R8"))
              	{
              		showAlert8();
              		
              	}
              	else
              	{
            	 Toast.makeText(MainActivity.this,"You clicked on block 8",Toast.LENGTH_LONG).show();
				 startActivityForResult(new Intent(MainActivity.this,Payment8Activity.class),8);
              	}
            break;
       
        }

  }
 
    public void showAlert1(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 1")
		    	.setMessage("Are you sure you want to vacate Block 1 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials1.class));
		    	    	 }
		    	})
		    	
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert2(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 2")
		    	.setMessage("Are you sure you want to vacate Block 2 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials2.class));
		    	    }
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert3(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 3")
		    	.setMessage("Are you sure you want to vacate Block 3 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials3.class));
		    	    }
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert4(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 4")
		    	.setMessage("Are you sure you want to vacate Block 4 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials4.class));
		    	    }
		    	    	
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert5(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 5")
		    	.setMessage("Are you sure you want to vacate Block 5 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials5.class));
		    	    }
		    	    
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert6(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 6")
		    	.setMessage("Are you sure you want to vacate Block 6 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials6.class));
		    	    }
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert7(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 7")
		    	.setMessage("Are you sure you want to vacate Block 7 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials7.class));
		    	    	
		    	    }
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    public void showAlert8(){
		MainActivity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		    	builder
		    	.setTitle("Release Block 8")
		    	.setMessage("Are you sure you want to vacate Block 8 ?")
		    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	    public void onClick(DialogInterface dialog, int which) {			      	
		    	    	startActivity(new Intent (MainActivity.this,CheckVacatingCredentials8.class));
		    	    }
		    	})
		    	.setNegativeButton("No", null)						//Do nothing on no
		    	.show();		    	       
		    	}
		    });		    	   		    	
		 }
    
    
    public void select()
    {
    	ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 
	nameValuePairs.add(new BasicNameValuePair("uid",uid));
	
    	try
    	{
		HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://tanushreedutta.site40.net/map/select.php");
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        HttpResponse response = httpclient.execute(httppost); 
	        HttpEntity entity = response.getEntity();
	        is = entity.getContent();
	        Log.e("pass 1", "connection success ");
	        
	}
        catch(Exception e)
	{
        	Log.e("Fail 1", e.toString());
        	runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
			    	Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
					
				}
			});
	}     
        
      try
        {
         	BufferedReader reader = new BufferedReader
				(new InputStreamReader(is,"iso-8859-1"),8);
            	StringBuilder sb = new StringBuilder();
            	while ((line = reader.readLine()) != null)
		{
       		    sb.append(line + "\n");
           	}
            	is.close();
            	result = sb.toString();
	        Log.e("pass 2", "connection success ");
	}
        catch(Exception e)
    	{
		Log.e("Fail 2", e.toString());
	}     
       
   	try
    	{
        	JSONObject json_data = new JSONObject(result);
        	status=(json_data.getString("status"));
        	
        	runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					if (uid.equals("1"))
					{
					if (status.equals("2"))
		        		 bv1.setText("R1");
					else
						bv1.setText("V1");
					}
					if (uid.equals("2"))
					{
					if (status.equals("2"))
		        		 bv2.setText("R2");
					else
						bv2.setText("V2");
					}
					if (uid.equals("3"))
					{

					if (status.equals("2"))
		        		 bv3.setText("R3");
					else
						bv3.setText("V3");
					}
					if (uid.equals("4"))
					{
					if (status.equals("2"))
		        		 bv4.setText("R4");
					else
						bv4.setText("V4");
					}
					if (uid.equals("5"))
					{
					if (status.equals("2"))
		        		 bv5.setText("R5");
					else
						bv5.setText("V5");
					}
					if (uid.equals("6"))
					{

					if (status.equals("2"))
		        		 bv6.setText("R6");
					else
						bv6.setText("V6");
					}
					if (uid.equals("7"))
					{
					if (status.equals("2"))
		        		 bv7.setText("R7");
					else
						bv7.setText("V7");
					}
					if (uid.equals("8"))
					{
					if (status.equals("2"))
		        		 bv8.setText("R8");
					else
						bv8.setText("V8");
					}
					
				}
			});  
	    	
    	}
        catch(Exception e)
    	{
        	Log.e("Fail 3", e.toString());
    	} 
    }
    
    public void update()
    {
    	
    	try
    	{
    		
    		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://tanushreedutta.site40.net/map/map.php");
	        nameValuePairs = new ArrayList<NameValuePair>(2);
	        
	       	nameValuePairs.add(new BasicNameValuePair("uid",uid));
	       	nameValuePairs.add(new BasicNameValuePair("status",status));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        response = httpclient.execute(httppost); 
	        ResponseHandler<String> responseHandler = new BasicResponseHandler();
			final String response = httpclient.execute(httppost, responseHandler);
	       // HttpEntity entity = response.getEntity();
	       // is = entity.getContent();
	        Log.e("pass 1", "connection success ");
    	}
        catch(Exception e)
    	{
        	Log.e("Fail 1", e.toString());
	    	Toast.makeText(getApplicationContext(), "Invalid IP Address",
			Toast.LENGTH_LONG).show();
    	}     
    }

}

On clicking V1 button (in my activity_main.xml file)it goes to Payment1Activity.java and does payment.


Java
package com.example.streetsystemparking;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RemoteViews;
//import com.example.streetsystemparking.MainActivity; 


public class Payment1Activity extends Activity {
	Button b,br1;
	EditText et,pass;
	TextView tv;
	HttpPost httppost;
	StringBuffer buffer;
	HttpResponse response;
	HttpClient httpclient;
	List<NameValuePair> nameValuePairs;
	ProgressDialog dialog = null;
	String status;
	String uid;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment1);
        b = (Button)findViewById(R.id.Button01);  
        et = (EditText)findViewById(R.id.accountno);
        pass= (EditText)findViewById(R.id.password);
       // tv = (TextView)findViewById(R.id.tv);
        
        b.setOnClickListener(new OnClickListener() {
        	
           
			@Override
			public void onClick(View v) {
				
				dialog = ProgressDialog.show(Payment1Activity.this, "", 
                        "Validating user...", true);
				 new Thread(new Runnable() {
					    public void run() {
					    	payment();					      
					    }
					  }).start();				
			}
		});
    }
	
	void payment(){
		try{			
			 
			httpclient=new DefaultHttpClient();
			httppost= new HttpPost("http://tanushreedutta.site40.net/payment_new/check.php"); // make sure the url is correct.
			//add your data
			nameValuePairs = new ArrayList<NameValuePair>(2);
			// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
			nameValuePairs.add(new BasicNameValuePair("accno",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
			nameValuePairs.add(new BasicNameValuePair("bpassword",pass.getText().toString().trim())); 
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			//Execute HTTP Post Request
			response=httpclient.execute(httppost);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			final String response = httpclient.execute(httppost, responseHandler);
			//System.out.println("Response : " + response); 
			runOnUiThread(new Runnable() {
			    public void run() {
			    	//tv.setText("Response from PHP : " + response);
				dialog.dismiss();
			    }
			});
			
			if(response.startsWith("User Found")){
				new Thread(new Runnable() {
				    public void run() {
				    	 
				    	update();					      
				    }
					
				  }).start();	
			
				runOnUiThread(new Runnable() {
				    public void run() {
				    	
				    	Toast.makeText(Payment1Activity.this,"Payment Successful for block 1", Toast.LENGTH_SHORT).show();
				    	Toast.makeText(Payment1Activity.this,"You reserved block 1",Toast.LENGTH_LONG).show();
				    	
                    setResult(1); 
				    }
				});
				startActivity(new Intent (Payment1Activity.this,VacatingCredentials.class));
				 
				
			}else{
				showAlert();		
			}
			
		}catch(Exception e){
			dialog.dismiss();
			System.out.println("Exception : " + e.getMessage());
		}
	}
	 public void update()
	    {
	    	try
	    	{
	    		uid="1";
				status="2";
			HttpClient httpclient = new DefaultHttpClient();
		        HttpPost httppost = new HttpPost("http://tanushreedutta.site40.net/map/map.php");
		        nameValuePairs = new ArrayList<NameValuePair>(2);
		        
		       	nameValuePairs.add(new BasicNameValuePair("uid",uid));
		       	nameValuePairs.add(new BasicNameValuePair("status",status));
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		        response = httpclient.execute(httppost); 
		        ResponseHandler<String> responseHandler = new BasicResponseHandler();
				final String response = httpclient.execute(httppost, responseHandler);
		       // HttpEntity entity = response.getEntity();
		       // is = entity.getContent();
		        Log.e("pass 1", "connection success ");
	    	}
	        catch(Exception e)
	    	{
	        	Log.e("Fail 1", e.toString());
		    	Toast.makeText(getApplicationContext(), "Invalid IP Address",
				Toast.LENGTH_LONG).show();
	    	}     
	    }
	public void showAlert(){
		Payment1Activity.this.runOnUiThread(new Runnable() {
		    public void run() {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(Payment1Activity.this);
		    	builder.setTitle("Payment Error.");
		    	builder.setMessage("User not Found.")  
		    	       .setCancelable(false)
		    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	        	   Toast.makeText(Payment1Activity.this,"Invalid Account number or Password,Try Again",Toast.LENGTH_LONG).show();
		    	        	    }
		    	       });		    	       
		    	AlertDialog alert = builder.create();
		    	alert.show();		    	
		    }
		});
	}
	
}


I am storing 1000 Rs in database as soon as the user registers with the system. Now what I want to implement is, as soon as the payment is successful it should deduct 100 Rs. from the users account (virtually) and send an sms stating "Deducted 100 Rs. Balance is 900" to the mobile no which a user had given during registration. Again if he tries to reserve another place then it should deduct 100 from 900 and so on. Can anyone help me with this ? Any suggestions or advices will be highly appreciated. Thank You.
Posted
Updated 17-Mar-14 23:18pm
v2
Comments
Richard MacCutchan 18-Mar-14 5:19am    
Too much code for anyone to figure out your problem. See my response to your repost of this question. BTW, please do not repost the same question; use the Improve question link to edit.

1 solution

Why send SMS when you can send notifications for free?

http://developer.android.com/guide/topics/ui/notifiers/notifications.html[^]
 
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