Tuesday, July 31, 2012

Saturday, July 28, 2012

Android TimerTask

Android TimerTask Example
TimerTask Represents the task will run specified time and it will run only once or Repeat.
Create new Class new TimerTask.
TimerTask Having two methods.

-->
1 .scheduledExecutionTime() // Repeat Task
2 .schedule() //Only once
Timer singleTask = new Timer();
Timer repeatTask = new Timer();

int singleTaskInterval = 3000; // 3 sec
int repeatInterval = 10000; // 10 sec

 // this task for specified time only once it will run
singleTask.schedule(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run 3 sec only once.
}
}, 1000);

  // this task for specified time it will run Repeat
repeatTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run every 10 sec repeat
}
}, 0, repeatInterval);

When your activity went to destroy or stop . you should cancel this task
--> -->
@Override
protected void onDestroy(){
super.onDestroy();
if(singleTask != null){
singleTask.cancel();
}
if(repeatTask != null){
repeatTask.cancel();
}
}
Activity Code
/**
*
* @author vijayakumar
*
*/
public class AndroidMADQAActivity extends Activity {
/** Called when the activity is first created. */
Timer singleTask = new Timer();
Timer repeatTask = new Timer();
int singleTaskInterval = 3000; // 3 sec
int repeatInterval = 10000; // 10 sec
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
singleTask.schedule(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run 3 sec only once.
}
}, 1000);
repeatTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Here do something
// This task will run every 10 sec repeat
}
}, 0, repeatInterval);
}
@Override
protected void onDestroy(){
super.onDestroy();
if(singleTask != null){
singleTask.cancel();
}
if(repeatTask != null){
repeatTask.cancel();
}
}
}



Thursday, July 5, 2012

Android mute and unmute sound

Android mute and unmute sound  
Below i write the code set device sound mute and unmute.
Problem.
For Ex: you are developing game application . if you want set game sound mute and unmute or anything else.
Solution.
Sound UnMute :
/**
* @author vijayakumar
*/

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Sound Mute:
/**
* @author vijayakumar
*/

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

Android Unzip Example

Android Unzip the in SD-Card
Below i write the code  for Download zip file from application server and Unzip to SD-Card. I Hope it will useful for you.
Activity Code: 
-->

/**

* @author vijayakumar

*/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
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;
public class AndroidQAActivity extends Activity {
private static Random random = new Random(Calendar.getInstance().getTimeInMillis());
private ProgressDialog mProgressDialog;
String unzipLocation = Environment.getExternalStorageDirectory() + "/Extract location Folder Name/Extracted Downloaded File";
String zipFile =Environment.getExternalStorageDirectory() "+/download zip file";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
DownloadMapAsync mew = new DownloadMapAsync();
mew.execute("Here Your Zip File Url");
}
class DownloadMapAsync extends AsyncTask<String, String, String> {
String result ="";
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(AndroidQAActivity.this);
mProgressDialog.setMessage("Downloading Zip File..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(zipFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.close();
input.close();
result = "true";
} catch (Exception e) {
result = "false";
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
if(result.equalsIgnoreCase("true")){
try {
unzip();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
}
}
}
public void unzip() throws IOException {
mProgressDialog = new ProgressDialog(AndroidQAActivity.this);
mProgressDialog.setMessage("Please Wait...Extracting zip file ... ");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
new UnZipTask().execute(zipFile, unzipLocation);
}
private class UnZipTask extends AsyncTask<String, Void, Boolean> {
@SuppressWarnings("rawtypes")
@Override
protected Boolean doInBackground(String... params) {
String filePath = params[0];
String destinationPath = params[1];
File archive = new File(filePath);
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, destinationPath);
}
UnzipUtil d = new UnzipUtil(zipFile, unzipLocation);
d.unzip();
} catch (Exception e) {
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
mProgressDialog.dismiss();
}
private void unzipEntry(ZipFile zipfile, ZipEntry entry,
String outputDir) throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
// Log.v("", "Extracting: " + entry);
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
} finally {
outputStream.flush();
outputStream.close();
inputStream.close();
}
}
private void createDir(File dir) {
if (dir.exists()) {
return;
}
if (!dir.mkdirs()) {
throw new RuntimeException("Can not create dir " + dir);
}
}}
}
Screen Shot




DOWNLOAD SOURCE CODE HERE

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...