my code stock.com

Snippet options

Download: Download snippet as mainactivityjava.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package com.example.bluwif;


import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	
	static boolean isFlashlightOn=false;
	static  boolean runnable_running=false;
	static boolean turn_on=false;
	static boolean no_value=true;
	static int frum_timer;
	static boolean params_on=false;
	ProgressBar progress;
	SeekBar seekbar;
	
	String out_string;
	String value_send;
	
	
	TextView simple_text, nocon_alert,text_view, inter;
	TextView sendval_text;
	NumberPicker num_pick;
	
	
	Parameters cameraParams;
	private Camera camera;

	 Animation animFadein,animFadeout;
	int activity_time=120000; //how much seconds application will run before closing.
	static String lol;
	
	@SuppressWarnings("deprecation")
	@Override
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		 
		inter = (TextView)findViewById(R.id.inter);
	
		final Button aprinde =(Button)findViewById(R.id.aprinde);
		Button refresh = (Button)findViewById(R.id.refresh_butt);
	
		nocon_alert = (TextView)findViewById(R.id.nocon_alert);
		
		num_pick = (NumberPicker)findViewById(R.id.num_pick); //NUMBER PICKER
		
		progress = (ProgressBar) findViewById(R.id.progressBar1);
		
		final ourHTTP hi = new ourHTTP(); 
		
		final GravitySensor sensor = new GravitySensor();
		
		////////////////
		  sensor.mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		   sensor.mSensorManager.registerListener(sensor.mSensorListener, sensor.mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
		  sensor.mAccel = 0.00f;
		  sensor.mAccelCurrent = SensorManager.GRAVITY_EARTH;
		  sensor.mAccelLast = SensorManager.GRAVITY_EARTH;
		
		/////////////
		 
		  
		num_pick.setVisibility(View.GONE);
		num_pick.setMinValue(1);
		num_pick.setMaxValue(8);
		num_pick.setWrapSelectorWheel(true);
		num_pick.setOnValueChangedListener( new NumberPicker.
	            OnValueChangeListener() {
	            @Override
	            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
	            	 
	            	String sendvalue;
	            		sendvalue= String.valueOf(newVal*50);
	            		
	            		lol=sendvalue;
	            		//no_value=false;
	            }
	        });
		
	   
	        
	//	seekbar.setVisibility(View.GONE);
		
		
		
		camera = Camera.open();
		final Runnable runnable;
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
			     .detectAll()
			     .penaltyLog()
			     .build();
			 StrictMode.setThreadPolicy(policy);
			
			 
			 //frum_timer=1000;
			 
			 runnable = new Runnable() {
			      @Override
			      public void run() {
			        for (int i = 1; i <= 40; i++) {
			          //final int value = i;
			         runnable_running=true;
			        
			         doFakeWork(frum_timer);
			         if(i==41){force_stop_led(); runnable_running=false;}  }
			         
			  
			         /*
			           progress.post(new Runnable() {
			               @Override
			               public void run() {
			            	  
			               }
			             }); */
			     
			        }
			      };
			    

			 
	new CountDownTimer(activity_time,300){																
		public void onTick(long millisUntilFinished){
								
			//getting info from server every 500 miliseconds.
			if(turn_on){
			out_string = hi.getWebPage("http://daniandroid.server07.com/register.php");  //getting interval value from site [ex. 500] which means 500 ms
			 //frum_timer = Integer.parseInt(out_string.replaceAll("[\\D]",""));   //for others type of data that you get from site/server.
		     try{
		    	 
		    	 frum_timer = Integer.parseInt(out_string);  //converts the interval to integer
		    	 nocon_alert.setText("h: "+ frum_timer);
		    	 
		    	 
		    	 if(frum_timer !=3 && runnable_running==false)
		    	 {
		    		 new Thread(runnable).start();
		    	 }
		     }catch(NumberFormatException e)
		     {
		    	 frum_timer=169;
		    	 //nocon_alert.setText(" "+ e.getMessage().toString());
		    	 e.printStackTrace();
		     }
		      
		      
			} }
		
			public void onFinish()
			{
				
			}
			}.start();
	
			
			
			 		
	
			    
	  new CountDownTimer(activity_time,500)
			    {
		  		
					@Override
					public void onTick(long millisUntilFinished) {
					if(turn_on){
						
							inter.setText(""+sensor.mAccelCurrent);
							if(sensor.mAccelCurrent <9 || sensor.mAccelCurrent >= 11)
							{   
								hi.sendToServer(lol);
							}
							else
							{
								hi.sendToServer("3");
							}
						
					}
					
						
						
						
						if(millisUntilFinished <3200)
						{
							Toast.makeText(getApplicationContext(), "Application going to sleep",
									Toast.LENGTH_LONG).show();
						}
					
					}
					
					@Override
					public void onFinish() {
						
						force_stop_led();
						//here we kill the process before 3-4 seconds of the remaining activity
						android.os.Process.killProcess(android.os.Process.myPid());
					}
			    }.start();
			    
			
		refresh.setOnClickListener(new View.OnClickListener() {
						
			@Override
			public void onClick(View v) {
				
		
			}
		});
		refresh.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				
				num_pick.setVisibility(View.VISIBLE);
				//hi.sendToServer("99");
				return true;
			}
		});
		
			
	aprinde.setBackgroundResource(R.drawable.turnoff);				
	 aprinde.setOnClickListener(new View.OnClickListener() {
							
		@Override
		public void onClick(View v) {
			
			if(turn_on==false)
			{
				v.setBackgroundResource(R.drawable.turnon);
				turn_on=true;
			}
			else
			{
				v.setBackgroundResource(R.drawable.turnoff);
				turn_on=false;
				
			}
			
		}
		});
	
	}
	

private void doFakeWork(int myfrum) {
	
	if(myfrum != 3){
    try {
    	Thread.sleep(myfrum);
    	start_Activity();
    }catch(final InterruptedException e)
    {
    	e.printStackTrace();
    	
    	
    }
    	 runOnUiThread(new Runnable() {

    		    @Override
    		    public void run() {
    		    	
    		            }
    	 });
      
  
	}
	
	
  }

@SuppressWarnings("deprecation")
public void start_Activity(){
	
	


	if(isFlashlightOn==false)
	{
		isFlashlightOn = true;
		params_on= true;
		if (camera ==null){ return; }
		
	cameraParams = camera.getParameters();
    cameraParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(cameraParams);
    camera.startPreview(); 
   
    
	}
	else
	{
		isFlashlightOn = false;
		 params_on = false;
		if (camera ==null){ return; }
		 	cameraParams.setFlashMode(Parameters.FLASH_MODE_OFF);
	        camera.setParameters(cameraParams);
	        camera.stopPreview();
	
	        force_stop_led();
	        
	}

	
}




public void force_stop_led()
{
	
	if (camera ==null){ return; }
	cameraParams.setFlashMode(Parameters.FLASH_MODE_OFF);
	try{
    camera.setParameters(cameraParams);
	}catch(RuntimeException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), e.getMessage().toString(), Toast.LENGTH_SHORT).show(); }
    camera.stopPreview(); 
    //camera.release();
   
}

public void Update(){
   
    
}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.