Wednesday 28 January 2015

change profile mode programatically

Change Android Profile Mode Pragmatically Using Android Incoming SMS



1. MainActivity

package com.androidexample.broadcastreceiver;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;


public class BroadcastNewSms extends Activity {

    TextView txt_message;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.androidexample_broadcast_newsms);
       
        txt_message = (TextView)findViewById(R.id.messagebox);
       
        Intent intent = getIntent();
       
          final AudioManager mode = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
       
        String phoneno = intent.getStringExtra("phoneno");
        String message = intent.getStringExtra("message");
       
        if(message !=null)
        {
           
            txt_message.setText("your message"+message+"and phone"+phoneno);
           
           
            if(message.equals("silent"))
            {
                mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Toast.makeText(getBaseContext(), "Silent Mode Activated",Toast.LENGTH_LONG).show();
            }
            if(message.equals("vibrate"))
            {
                mode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
               
                mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Toast.makeText(getBaseContext(), "Vibrate Mode Activated",Toast.LENGTH_LONG).show();
            }
            if(message.equals("normal"))
            {
                mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
               
                mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Toast.makeText(getBaseContext(), "Vibrate Mode Activated",Toast.LENGTH_LONG).show();
            }
           
        }
       
    }
}



2. IncomingSMS ----->Broadcast Receiver

package com.androidexample.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class IncomingSms extends BroadcastReceiver {
   
    // Get the object of SmsManager
     String message ="";
     String senderNum = "";
   
    final SmsManager sms = SmsManager.getDefault();
   
    public void onReceive(Context context, Intent intent) {
   
        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {
           
            if (bundle != null) {
               
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
               
                for (int i = 0; i < pdusObj.length; i++) {
                   
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                   
                    senderNum = phoneNumber;
                    message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
                   
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();
                   
                } // end for loop
               
                 if(message !=null) {
                        // notify new arriving message
                        Toast.makeText( context, message, Toast.LENGTH_SHORT ).show();
                        // set data to send
                        Intent data = new Intent(context, BroadcastNewSms.class);
                        // new activity
                        data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                        data.putExtra("phoneno", senderNum);
                        data.putExtra("message", message);
                        // start
                        context.startActivity(data);
                    }
               
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);
           
        }
    }
   
   
}


3. Manefest --->xml file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidexample.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
      
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidexample.broadcastreceiver.BroadcastNewSms"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">  
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
       
    </application>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
   
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
   
</manifest>





Main Activity Xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BroadcastPhoneStates" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="When new SMS will come it will show a alert message." />

    <TextView
        android:id="@+id/messagebox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</RelativeLayout>




Screen shot

Silent Mode change:








Vibrate Mode Change







No comments:

Post a Comment