Wednesday, January 21, 2015

Creating Boot Receiver in android.....


This article shows how to create Boot Receiver in Android.

1- Create BootReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
//Make your stuff
}

}

2- Register it to Menifest.xml

<receiver android:name="com.example.BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

2- Write this permission to Menifest.xml

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

Wednesday, December 17, 2014

Creating Shared Preferences in android.....


This article shows how to create SharedPreferences in android.


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class MyPreferences {
    public static boolean putInt(Context activity,String key,int value){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();                      
        return true;      
    }
 
    public static int getInt(Context activity,String key,int defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        int temp = sharedPreferences.getInt(key, defaultValue);
        return temp;      
    }
 
    public static boolean putString(Context activity, String key, String value)
    {
    SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();                      
        return true;         }
 
    public static String getString(Context activity,String key,String defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        String temp = sharedPreferences.getString(key, defaultValue);
        return temp;      
    }
 
    public static boolean putLong(Context activity, String key, long value)
    {
    SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
return true;         }
 
    public static long getLong(Context activity,String key,long defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        long temp = sharedPreferences.getLong(key, defaultValue);
        return temp;      
    }
 
}

By implementing above code you can easily Create SharedPreferences.

Thursday, December 4, 2014

Creating notification in android.....


This article shows how to create notification in android.


public MyNotification(Context ctx){
super();
this.ctx=ctx;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
notification=builder.getNotification();
notification.when=when;
notification.icon=R.drawable.mp3_icon;
remoteViews=new RemoteViews(ctx.getPackageName(),                        R.layout.service_layout);
notification.contentView = remoteViews;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(5, notification);
}



Wednesday, December 3, 2014

Creating Custom views in a PreferenceActivity in android...



This article shows how to add custom view in PreferenceActivity in android.

Create main.xml, the only necessary view is a ListView, with id: android:id="@android:id/list".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:weightSum="1">
        <ListView 
            android:id="@android:id/list" 
            android:layout_weight="1"
            android:layout_width="fill_parent"
                android:layout_height="0dp">
        </ListView>
        <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


Create CustomPreferenceActivity.java

public class CustomPreferenceActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                addPreferencesFromResource(R.xml.settings);
               
                //setup any other views that you have
                TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("View Added");
        }
}

Sunday, September 28, 2014

Handling Looper.prepare() in android.....


This article shows how to handle Looper.prepare() in android.

Show Toast inside Thread by using UiThread.


new Thread() {
        @Override
        public void run() {
               try {
                     // code runs in a thread
                     runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                     });
               } catch (final Exception ex) {
                   Log.i("---","Exception in thread");
               }
        }
 }.start();
This will avoid Looper.prepare() in android.

Wednesday, September 3, 2014

Installing & Uninstalling Home Screen Shortcut.....


This Article shows to create installing & uninstalling home screen shortcut.

1- Create method for adding shortcut to home screen(addShortcut()).

private void addShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
add.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(add);
    }

2- Create method for removing shortcut from home screen(removeShortcut()).

private void removeShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(add);
    }

3- Add following permissions to Manifest.xml.

<uses-permission        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission          android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Thursday, August 14, 2014

Creating SimpleAdapter for ListView in Android.....


This Article shows to create SimpleAdapter for ListView in Android.

1- Create row xml for ListView(row.xml).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <TextView android:id="@+id/text1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView android:id="@+id/text2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
     <TextView android:id="@+id/text3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
</LinearLayout>


2- Create ListActivity(MainActivity.java).

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Abc extends ListActivity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> list =
                  new ArrayList<HashMap<String, String>>();

String[] from = new String[] {"Text1", "Text2", "Text3"};

int[] to = new int[] {R.id.text1, R.id.text2, R.id.text3};
for(int i=0; i<3; i++) {
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
map.put(from[i], "Text" + i);
list.add(map);
}
setListAdapter(new SimpleAdapter(this, list, R.layout.row, from, to));
}
}