Search...

Tuesday, March 20, 2012

How to Listen For Incoming SMS Messages in Android?

Step1



<uses-permission id="android.permission.RECEIVE_SMS" />

    <application>

        <receiver class=".TestSMSApp">
            <intent-filter>

                <action android:value="android.provider.Telephony.SMS_RECEIVED" />

            </intent-filter>
        </receiver>

    </application>

Step2:



package org.apache.sms;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.os.Bundle;
import android.provider.Telephony;
import android.util.Log;
import android.telephony.gsm.SmsMessage;

public class TestSMSApp extends IntentReceiver {
    private static final String LOG_TAG = "TestSMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {

           Bundle bundle = intent.getExtras();      
        SmsMessage[] msgs = null;
        String str = "";          
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];          
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);              
                str += "SMS from " + msgs[i].getOriginatingAddress();                    
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";      
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
         
            }
         
          }
        }
   }

   




How to Retrieve incoming call’s phone number in Android?

Step1: Add Below code in your manifest file


 <receiver android:name=".CallBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />    
        </intent-filter>
</receiver>

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />


Step2: Create class to receive Broadcast message




public class CallBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   
 String action = intent.getAction();

           if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
              if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                                  TelephonyManager.EXTRA_STATE_RINGING)) {
                  //Incoming call
               doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
              }
}



Thursday, March 15, 2012

How to changing the ringer volume in Android?


Step1:

Create one layout with seekbar view(main.xml)

Step2:



import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class VolumeActivity extends Activity
{

//a variable to store the seek bar from the XML file
private SeekBar volumeBar;

//an AudioManager object, to change the volume settings
private AudioManager amanager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //get the seek bar from main.xml file
        volumeBar = (SeekBar) findViewById(R.id.sb_volumebar);

        //get the audio manager
        amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

        //seek bar settings//
        //sets the range between 0 and the max volume
        volumeBar.setMax(amanager.getStreamMaxVolume(AudioManager.STREAM_RING));
        //set the seek bar progress to 1
        volumeBar.setKeyProgressIncrement(1);

        //sets the progress of the seek bar based on the system's volume
        volumeBar.setProgress(amanager.getStreamVolume(AudioManager.STREAM_RING));

        //register OnSeekBarChangeListener, so that the seek bar can change the volume
volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}

@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//change the volume, displaying a toast message containing the current volume and playing a feedback sound
amanager.setStreamVolume(AudioManager.STREAM_RING, progress, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
});
    }

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if one of the volume keys were pressed
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
//change the seek bar progress indicator position
volumeBar.setProgress(amanager.getStreamVolume(AudioManager.STREAM_RING));
}
//propagate the key event
return super.onKeyDown(keyCode, event);
}
}

How to close/hide the Android Soft Keyboard?



You can force Android to hide the virtual keyboard using the InputMethodManager, callinghideSoftInputFromWindow, passing in the token of the window containing your edit field.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Monday, March 12, 2012

How to use AlarmManager to update widget(remoteview) in Android?

How to make widget(remoteview) in Android?

How to find out that service is running or not in Android?


private boolean isMyServiceRunning() {
   ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
   for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
       if ("com.test.DataService".equals(service.service.getClassName())) {
           return true;
       }
   }
   return false;
}