Monday, 23 September 2013

Send SMS in Android

This blog will show you how to send SMS and get delivery report.
Step 1:  Declare these BroadcastReceivers. 

BroadcastReceiver sendBroadcastReceiver = new sentReceiver();
    BroadcastReceiver deliveryBroadcastReciever = new deliverReceiver();;

 Step2: Include these inside your button click or anywhere:
 
 send_sms.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub 
                         sendSMS("phone_no", "sms_content");
                        finish();
            }
        });
Step 3: Implement the sendSMS method
 
private void sendSMS(String phoneNumber, String message) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

        registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);


    } 
Step4:   Declare the deliverReceiver class
 
class deliverReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), sms_delivered,
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), sms_not_delivered,
                        Toast.LENGTH_SHORT).show();
                break;
            }

        }
    }
Step 5: The same way declare the sentReceiver class.

    class sentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), sms_sent, Toast.LENGTH_SHORT)
                        .show();
                startActivity(new Intent(SendSMS.this, ChooseOption.class));
                overridePendingTransition(R.anim.animation, R.anim.animation2);
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
            }

        }
    }
 
Step 6: The important thing is to destory the receivers, while moving the next activity. 
 
@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        try {
            unregisterReceiver(sendBroadcastReceiver);
            unregisterReceiver(deliveryBroadcastReciever);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    try {
        unregisterReceiver(sendBroadcastReceiver);
        unregisterReceiver(deliveryBroadcastReciever);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 

Wednesday, 20 June 2012

Load items when scroll reaches the end in ListView

listview.setOnScrollListener(new OnScrollListener() {
            private int threshold = 0;
            private int previousTotal = 0;
            private boolean loading = true;

            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                } else {
                    Log.v("in gridview loading more","grid load");
                    if ((totalItemCount - visibleItemCount) == (firstVisibleItem + threshold)) {
                        // add items to your listview
                                        }
                }

            }
        });

Saturday, 4 February 2012

List all apk files present in exernal directory

The following code will list all the .apk files present in your external directory and ask us to install in the device.


public class ListApkActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExtFilter apkFilter = new ExtFilter("apk");
        File file[] = Environment.getExternalStorageDirectory().listFiles(apkFilter);
        Log.d("InstallApk", "Filter applied. Size: "+ file.length);

        for (int i=0; i < file.length; i++)
        {
            Log.d("InstallApk", "FileName:" + file[i].getName());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file[i]),  
    "application/vnd.android.package-archive");
            startActivity(intent);
        }
    }

    class ExtFilter implements FilenameFilter {  // class which list all the files having .apk extension
        String ext;
        public ExtFilter(String ext) {
            this.ext = "." + ext;
        }
        public boolean accept(File dir, String name) {
            return name.endsWith(ext);
        }
    }
    }