Friday, April 27, 2012

How to Make a Phone Call Programmatically in Android

 -------------------------------------------
App Name: MakeACall
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------


MyActivity.java


package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyActivity extends Activity implements OnClickListener {

private Button btncall = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btncall = (Button) findViewById(R.id.btnCall);
btncall.setOnClickListener(this);
}

@Override
public void onClick(View v) {
String mobileNo = "+919741817902";
String uri = "tel:" + mobileNo.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
}

main.xml




AndroidManifest.xml
















The output Screen will be like this..


You can download the complete source code zip file here : MakeACall

 cheers!!

 I'd love to hear your thoughts!

Labels: , ,

Sunday, April 15, 2012

Start Service from Activity


Some time we need to start a service from Android Activity..
how can we achieve this i am going to write step by step.

First of all we need to Create a simple application with an Activity

-------------------------------------------
App Name: Activity2Service
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------


"MyActivity.java"
 
package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Log.v("Debug", "Activity has been Started..");
Toast.makeText(getBaseContext(),
"Activity Started", Toast.LENGTH_SHORT)
.show();
Intent myIntent = new Intent(getBaseContext(), MyService.class);
startService(myIntent);
}
}

Then Create a Service
  
package com.rdc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
Log.v("Debug", "Service has been Created..");
// code to execute when the service is first created
}

@Override
public void onDestroy() {
// code to execute when the service is shutting down
}

// This method has been deprecated since API 5
/*
@Override public void onStart(Intent intent, int startid) {
code to execute when the service is starting up
}*/


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Debug", "Service has been Started..");
Toast.makeText(getBaseContext(), "Service has been Started..",
Toast.LENGTH_SHORT).show();
// We want this service to continue running until it's explicitly
// stopped, so return sticky.
return START_STICKY;
}
}

main.xml

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


Don't forget to make entry in "Manifest" file for Service
  
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:enabled="true"
android:name=".MyService">
<intent-filter>
<action android:name="com.rdc.MyService">
</action>
</intent-filter>
</service>

</application>

</manifest>

Now Run Application, Toast will appear..  :)

You can download the complete source code zip file here : Activity2Service

cheers!!

I'd love to hear your thoughts!

Labels: , ,

Saturday, April 7, 2012

Start Activity from Activity


Some time we need to start an Activity from Activity..
how can we achieve this i am going to write step by step.

We will create very simple app to do this work
 -------------------------------------------
App Name: Activity2Activity
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: FirstActivity
-------------------------------------------


FirstActivity.java

package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);

Button btnload = (Button) findViewById(R.id.btnfirst);
btnload.setOnClickListener(this);

}

@Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
}

SecondActivity.java
package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

Button btnload = (Button) findViewById(R.id.btnsecond);
btnload.setOnClickListener(this);

}

@Override
public void onClick(View v) {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}
}

first.xml

<?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">
<TextView
android:layout_width="fill_parent"
android:text="I am first activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load Second Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnfirst"></Button>
</LinearLayout>

</LinearLayout>



second.xml

<?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">
<TextView
android:layout_width="fill_parent"
android:text="I am Second activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load First Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnsecond"></Button>
</LinearLayout>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".FirstActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

The output Screen will be like this..


You can download the complete source code zip file here : Activity2Activity

 cheers!!

 I'd love to hear your thoughts!

Labels: , ,

Sunday, April 1, 2012

Start BroadcastReceiver from Activity

Some time we need to start a service from Android Activity..
how can we achieve this i am going to write step by step.

We will create very simple app to do this work
 -------------------------------------------
App Name: Activity2BReceiver
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------


MyActivity.java
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("Debug", "Activity has been started..");
}
}

Then Create a Broadcast Receiver

MyReceiver.java
  
package com.rdc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.v("Debug", "SMS Broadcast Receiver has been started..");
Toast.makeText(context, "BReceiver is watching ur message..",
Toast.LENGTH_SHORT).show();

}

}

main.xml

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


Don't forget to make entry in "Manifest" file for Receiver
  
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MyActivity"
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.rdc.MyReceiver">

<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

</application>
</manifest>

Run this app and send message from another mobile/emulator, Receiver will notify you about new sms
See below output screens tensted on Emulator and Real Device


You can download the complete source code zip file here : Activity2BReceiver


I'd love to hear your thoughts!

Labels: , ,