Boot Reciever


A boot reciever is a listener that picks up an intent that is sent when the phone boot animation has stoped. the first thing that needs to be done with it is register it in the manifest

<uses-permission android:name=“android.permission.RECEIVE_BOOT_COMPLETED”/>

<receiver android:name=“com.lukemovement.example.BootResiever”>

<intent-filter>

<action android:name=“android.intent.action.BOOT_COMPLETED”/>

<action android:name=“android.intent.action.QUICKBOOT_POWERON”/>

</intent-filter>

</receiver>

the next bit is creating the activity that responds when the boot complete intent is recieved

package com.lukemovement.example;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

public class BootResiever extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

    {

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

//intent to start a new main activity

Intent i = new Intent(context, BootActivity.class);

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(i);

}

  }

}

and it is as simple as that

Check File


This is how you can check if a pesific fil exists on your sdcard, this will all so work for you system directory but not the data derectory as that an only be read with super user permissions(also cant be read by an api).

boolean exists1 = (new File(Environment.getExternalStorageDirectory() + “/example/1″)).exists();

if (exists1) {

}

Alternatively this methord can be used to create a new folder or delete files/directorys

File a = new File(Environment.getExternalStorageDirectory() + “/example1/example2″);

a.mkdir();//this will create example2 only

a.mkdirs();//this will create example1 and example2

a.delete();//this will delete example2 weather it is a file or a folder

 

Posted in Java. 3 Comments »

Check Internet Connection


this is the simplest way i can find that you can check for an internet connection.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo Wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo Network = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (Wifi.isConnected()) {
Toast msg1 = Toast.makeText(advanced.this,
“Wifi is connected”, Toast.LENGTH_SHORT);
msg1.show();
} else if {Network.isConnected()) {
Toast msg1 = Toast.makeText(advanced.this,
“Mobile data is connected”, Toast.LENGTH_SHORT);
msg1.show();
} else {
Toast msg1 = Toast.makeText(advanced.this,
“No connection to the internet available”, Toast.LENGTH_SHORT);
msg1.show();
}

and the permission for the manafest need to include these (First one, only if you want to use the internet)

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

Request Text Input


This is an example taken from my new app (App2System PRO) of how a dialog can be used to request user input.

These strings need to be announsed at the start of the Activity to allow the to be used throughout it.

String value;
String New_Value;

The rest of this, being a dialog, can be put in the onCreate bundle or in a public void.

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(“Title”);
alert.setMessage(“Message”);

// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
New_Value = value.replaceAll(” “, “_”);//this is an example of how strings can be eddited at run time.
if (New_Value.equals(“”)) {

//here you can put what you want done if the text field was left empty.
Intent i = new Intent(Main2.this, Main1.class);
startActivity(i);
finish();
Toast msg1 = Toast.makeText(Install_System.this,
“Please Enter Some Text”, Toast.LENGTH_LONG);
msg1.show();
} else {
//do something with the string/text

Toast msg1 = Toast.makeText(Install_System.this,
“was ” + value “, now ” + New_Value, Toast.LENGTH_LONG);
msg1.show();
}
}
});
alert.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent i = new Intent(main2.this, main1.class);
startActivity(i);
finish();
}
});
alert.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Intent i = new Intent(main2.this, main1.class);
startActivity(i);
finish();
}
});

alert.show();
}

Splash Screen


this is an example of an splashscreen i found on the internet, so i take no credit for this.

 

Image

Description: The code is somewhat self-expressive. Dont forget to add this new Activity in the AndroidManifest.xml :!:

“/res/layout/splashscreen.xml”:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
  3.    android:orientation=”vertical”
  4.    android:layout_width=”fill_parent”
  5.    android:layout_height=”fill_parent”
  6.    >
  7.     <ImageView
  8.         android:layout_width=”fill_parent”
  9.         android:layout_height=”fill_parent”
  10.         android:scaleType=”fitCenter”
  11.         android:src=”@drawable/place_your_splashimage_here”
  12.         />
  13. </LinearLayout>

“/src/your_package_structure/Splash.java”:

Download

  1. package org.anddev.android.andnav;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. public class Splash extends Activity {
  7.         // ===========================================================
  8.         // Fields
  9.         // ===========================================================
  10.         private final int SPLASH_DISPLAY_LENGHT = 1000;
  11.         // ===========================================================
  12.         // “Constructors”
  13.         // ===========================================================
  14.         /** Called when the activity is first created. */
  15.         @Override
  16.         public void onCreate(Bundle icicle) {
  17.                 super.onCreate(icicle);
  18.                 setContentView(R.layout.splashscreen);
  19.                 /* New Handler to start the Menu-Activity
  20.                  * and close this Splash-Screen after some seconds.*/
  21.                 new Handler().postDelayed(new Runnable(){
  22.                         @Override
  23.                         public void run() {
  24.                                 /* Create an Intent that will start the Menu-Activity. */
  25.                                 Intent mainIntent = new Intent(Splash.this,Menu.class);
  26.                                 Splash.this.startActivity(mainIntent);
  27.                                 Splash.this.finish();
  28.                         }
  29.                 }, SPLASH_DISPLAY_LENGHT);
  30.         }
  31. }

 

Original Post By plusminus at www.anddev.org

Progress Dialog


this is how to properly display a progress dialog when you need to preform a long task. though i would upload this example as it took me a good couple of weeks to find out how to do this properly.

pd = ProgressDialog.show(advanced.this, getString(R.string.Updating_Busybox), getString(R.string.Please_Wait));
pd.setCancelable(true);
new Thread(new Runnable(){
@Override
public void run() {

//Big task goes here!

}}).start();

this nees to be announsed BEFORE the onCreate bundle to declare “pd”.

private ProgressDialog pd;

Posted in Java. 1 Comment »

Download files


this will allow you to download a file from the internet, this has been copy from part of my app called “Root Toolbox Pro”.

try {
URL input = new URL(“http://dl.dropbox.com/u/22618820/open%20source/Toolbox/in%20app%20downloads/Gapps-2.3-Universal.zip&#8221;);
URLConnection conn = input.openConnection();
int contentLength = conn.getContentLength();

DataInputStream stream = new DataInputStream(input.openStream());

byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();

DataOutputStream output = new DataOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory() + “/Gapps-Update.zip”));
output.write(buffer);
output.flush();
output.close();
} catch(FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}

also these permission will need to be added to the manafest to allow your application to access the internet and save files to te sdcard.

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”></uses-permission>

Follow

Get every new post delivered to your Inbox.