我需要阻止来电和短信并自动回复 [英] i need the to block the calls and sms and auto respond back

查看:71
本文介绍了我需要阻止来电和短信并自动回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package za.ac.unizulu.cs.smartdrivedemo;

package za.ac.unizulu.cs.smartdrivedemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Formatter;
import java.util.Locale;

public class SpeedActivity extends Activity implements IBaseGpsListener{
	
	boolean activateSMSInterceptor, activateCallInterceptor = false;
	
	private float nCurrentSpeed;

	TextView txtCurrentSpeed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_speed);
		
		/*Button startButton = (Button) findViewById(R.id.startButton);
		startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		/*Button stopButton = (Button) findViewById(R.id.stopButton);
		stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

	    this.updateSpeed(null);
		
	}
	
	
	public void updateSpeed(CLocation location)
	  {
	    nCurrentSpeed = 0;

	    if( location!=null )
	    {
	      location.setUseMetricUnits(this.useMetricUnits());
	      nCurrentSpeed = location.getSpeed();
	      
	      //adjust nCurrentSpeed to reflect the approx 10km/h GPS inaccuracy
	      //if (nCurrentSpeed >= 95){
	    	  //adds approx 10% to the speed measured by the GPS
	    	  nCurrentSpeed = nCurrentSpeed + (nCurrentSpeed/10); 
	    	  
	    	  
	    	  
	      //}
	    }

		//for testing purposes on the emulator
	  	//nCurrentSpeed = 6.3f;
	    
	    @SuppressWarnings("resource")
		Formatter fmt = new Formatter(new StringBuilder());
	    fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
	    String strCurrentSpeed = fmt.toString();
	    strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

	    String strUnits = "mph";
	    if (this.useMetricUnits())
	    {
	      strUnits = "km/h";
	    }

	    txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
	    txtCurrentSpeed.setTextColor(Color.BLACK);
	    //changes the font size based upon the speed being traveled
	    //txtCurrentSpeed.setTextSize((nCurrentSpeed + 10) * 0.5f);
	    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
	    
	    //deploys the relevant notifications
	    deployService(nCurrentSpeed);	    
	  }

	  private void deployService(float currentSpeed) {
		// TODO Auto-generated method stub
		  if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				//Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
		  }
		  else if(currentSpeed >=80){
			  Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=5.1){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Enable",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				
				//ensures that the interceptors start once only
				if(activateCallInterceptor == false){
					//TODO add call interception service initiation here
					startService(new Intent(SpeedActivity.this,CallInterceptorService.class));
					activateCallInterceptor = true;
				}
				
				if(activateSMSInterceptor == false){
					startService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); // this service works
					//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = true;
				}
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_LONG).show();
				//enableSMSReception();
				
				//ensures that the interceptors end once only
				if(activateCallInterceptor == true){
					stopService(new Intent(SpeedActivity.this, CallInterceptorService.class));
					activateCallInterceptor = false;	
				}
				if(activateSMSInterceptor == true){
					stopService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); //this service works
					//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = false;
				}
			}
	}


	public boolean useMetricUnits()
	  {
	    /*CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
	    return chkUseMetricUnits.isChecked();*/
		  return true;
	  }
	  
	  /*public void displayToasts(float currentSpeed){
			if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
			}
			else if(currentSpeed >=80){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=20){
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_SHORT).show();
				//enableSMSReception();
			}
			
		}*/
	

	/*@Override
	public void finish()
	  {
	    super.finish();
	    System.exit(0);
	  }*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.speed, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
	    {
	      CLocation myLocation = new CLocation(location, this.useMetricUnits());
	      this.updateSpeed(myLocation);
	    }
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGpsStatusChanged(int event) {
		// TODO Auto-generated method stub
		
	}

}

推荐答案

package za.ac.unizulu.cs.smartdrivedemo;



package za.ac.unizulu.cs.smartdrivedemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Formatter;
import java.util.Locale;

public class SpeedActivity extends Activity implements IBaseGpsListener{
	
	boolean activateSMSInterceptor, activateCallInterceptor = false;
	
	private float nCurrentSpeed;

	TextView txtCurrentSpeed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_speed);
		
		/*Button startButton = (Button) findViewById(R.id.startButton);
		startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		/*Button stopButton = (Button) findViewById(R.id.stopButton);
		stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

	    this.updateSpeed(null);
		
	}
	
	
	public void updateSpeed(CLocation location)
	  {
	    nCurrentSpeed = 0;

	    if( location!=null )
	    {
	      location.setUseMetricUnits(this.useMetricUnits());
	      nCurrentSpeed = location.getSpeed();
	      
	      //adjust nCurrentSpeed to reflect the approx 10km/h GPS inaccuracy
	      //if (nCurrentSpeed >= 95){
	    	  //adds approx 10% to the speed measured by the GPS
	    	  nCurrentSpeed = nCurrentSpeed + (nCurrentSpeed/10); 
	    	  
	    	  
	    	  
	      //}
	    }

		//for testing purposes on the emulator
	  	//nCurrentSpeed = 6.3f;
	    
	    @SuppressWarnings("resource")
		Formatter fmt = new Formatter(new StringBuilder());
	    fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
	    String strCurrentSpeed = fmt.toString();
	    strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

	    String strUnits = "mph";
	    if (this.useMetricUnits())
	    {
	      strUnits = "km/h";
	    }

	    txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
	    txtCurrentSpeed.setTextColor(Color.BLACK);
	    //changes the font size based upon the speed being traveled
	    //txtCurrentSpeed.setTextSize((nCurrentSpeed + 10) * 0.5f);
	    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
	    
	    //deploys the relevant notifications
	    deployService(nCurrentSpeed);	    
	  }

	  private void deployService(float currentSpeed) {
		// TODO Auto-generated method stub
		  if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				//Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
		  }
		  else if(currentSpeed >=80){
			  Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=5.1){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Enable",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				
				//ensures that the interceptors start once only
				if(activateCallInterceptor == false){
					//TODO add call interception service initiation here
					startService(new Intent(SpeedActivity.this,CallInterceptorService.class));
					activateCallInterceptor = true;
				}
				
				if(activateSMSInterceptor == false){
					startService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); // this service works
					//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = true;
				}
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_LONG).show();
				//enableSMSReception();
				
				//ensures that the interceptors end once only
				if(activateCallInterceptor == true){
					stopService(new Intent(SpeedActivity.this, CallInterceptorService.class));
					activateCallInterceptor = false;	
				}
				if(activateSMSInterceptor == true){
					stopService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); //this service works
					//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = false;
				}
			}
	}


	public boolean useMetricUnits()
	  {
	    /*CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
	    return chkUseMetricUnits.isChecked();*/
		  return true;
	  }
	  
	  /*public void displayToasts(float currentSpeed){
			if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
			}
			else if(currentSpeed >=80){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=20){
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_SHORT).show();
				//enableSMSReception();
			}
			
		}*/
	

	/*@Override
	public void finish()
	  {
	    super.finish();
	    System.exit(0);
	  }*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.speed, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
	    {
	      CLocation myLocation = new CLocation(location, this.useMetricUnits());
	      this.updateSpeed(myLocation);
	    }
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGpsStatusChanged(int event) {
		// TODO Auto-generated method stub
		
	}

}


这篇关于我需要阻止来电和短信并自动回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆