Monday, 29 August 2011

Install/Uninstall App from code

Hi ,
In this post i am going to tell you about how  to uninstall the app from the code.

INTENT FILTERS FOR SUCH THING  IS AS FOLLOWS:


<activity android:name=".PackageInstallerActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="content" />
        <data android:scheme="file" />
        <data android:mimeType="application/vnd.android.package-archive" />
    </intent-filter>
</activity>
 
<activity android:name=".UninstallerActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.DELETE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package" />
    </intent-filter>
</activity>
 
  
FOR UNINSTALLING THE APP:

Uri packageURI = Uri.parse("package:com.android.name_of_your_app");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
 

FOR  INSTALLING THE APP:(To install apk from your SD card)

String fileName = Environment.getExternalStorageDirectory() + "/name_of_your_app.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

 

Friday, 19 August 2011

Fetch phone book contacts information

Hi, in this tutorial i am going to tell you about how to fetch Andorid phonebook contacts.
Previously people  class was used to fetch phone book contacts, but in later versions we use ContactsContract.
(Remember that you follow these steps in proper order)

Step1:)First make the object of content resolver class(This class provides applications access to the content model.)

ContentResolver cr = getContentResolver();

Step2:)Now create a cursor which will fetch data from ContactsContract  class.

// this cursor will contain whole data
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

Step3): Now we have all the data in the cursor so now we can fetch the required data from the cursor as follows:(in this step i am fetching all the names and the id's)


while (cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


NAME.add(name);  //all names are added in the arraylist named "NAME".
ID.add(id);  //all names are added in the arraylist named "ID".
}

Step4):Now if you want to fetch further information like phone number or email.it can be fetched as follows:
To fetch Phone numbers of all contacts:

Cursor cphon=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);

                while(cphon.moveToNext())
                { 

//all  phone numbers will be stored in the arraylist named "PHONE_NUM"                    PHONE_NUM=cphon.getString(cphon.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                 }




To fetch Email id's of all contacts:


Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);

                while (emailCur.moveToNext()) {
                    String email = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    EMAIL_ID.add(email); //I am saving all emails in arraylist named  "EMAIL_ID"
                    String emailType = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                }
                emailCur.close();







thats it..........................

Change the Ringer Mode

Today i am going to write about changing the ringer mode of the Android phone programmitically.
Quite Simple: :-)

Step1):Make an object of Audio Manger class
                    AudioManager am;
                    am = (AudioManager)
                    context.getSystemService(Context.AUDIO_SERVICE);



Step2)Now after you have created the object of audio manager class you can at any time change the Ringer mode as follows:

For Silent Mode:
am.setRingerMode(0);        


For Vibrate mode:

am.setRingerMode(1);        


For Normal mode:

am.setRingerMode(2);       


:-)

Thursday, 18 August 2011

Android Application Global class

Hi in this blog i am going to write about the application global class.
You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag.
Below is the code tutorial of application global class:
Step1:) Create a new class which extends the application class as follows:


public class ApplicationSetterClass extends Application{
    private String mname;
    private String mage;
    // This Method will get the name  saved in application class
    public String getName()
    {
        return mname;
    }

    // This Method will set the name in application class
    public void setName(String mname)
    {
        this.mname=mname;
    }

    // This Method will get the age  saved in application class
    public String getAge()
    {
        return mage;
    }

    public void setAge(String mage)
    {
        this.mage=mage;
    }
}

Step2):Now  declare your application gobal class in application tag in android  manifest file as follows:

<application android:icon="@drawable/icon" android:name="ApplicationSetterClass" android:label="@string/app_name">
</application>


Step3)Now your applciation global class is ready to save the application state:Make a object of the ApplicationSetterClass in your activty where you want to use it as follows:

ApplicationSetterClass  appset=(ApplicationSetterClass) getApplication(); 


Now you can save any data as follows:

 appset.setName("Your droid tutorial");
 appset.setAge("21");


And now this data will be available  globally to whole application:
You can retrieve data as follows:


appset.getName();
appset.getAge();


Source code:https://sites.google.com/site/quickandroidblog/download-stuff

Android tutorial to Change horizontal progress bar color and border

In this Android Tutorial i will tell you how to create your own custom progress bar 
Below are the steps to create your own custom progress bar.
 Just follow the steps :

STEP - 1)

main.xml:(add this to your main.xml):

<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="121dp"
android:layout_height="15dp"
android:progress="50"
android:max="100"
android:secondaryProgress="0"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/myprogressbar"
android:layout_marginTop="10dp"
/>
</LinearLayout>


 
 STEP - 2) 
myprogressbar.xml (save this xml in drawable folder)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="25dip" />
            <gradient android:startColor="#C0C0C0" android:centerColor="#F8F8FF"
                android:centerY="0.75" android:endColor="#ffffff" android:angle="90" />
            <stroke android:width="1dp" android:color="#6B8E23" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="25dip" />
                <gradient android:startColor="#9ACD32" android:endColor="#FFFF00"
                    android:angle="90" />
                <stroke android:width="1dp" android:color="#6B8E23" />
            </shape>
        </clip>
    </item>
</layer-list>