26 November 2015

Launch system apps from your Android app

What we will do?
->We will launch default (in-built) calculator in the android app you have made.

What we will need?
  • Button in activity_main.xml
  • Emulator or mobile with default calculator
  • Changes in MainActivity.java
Step 1 : Create a new application. Go to File->New->Android Application Project.
file new android app

Step 2 : Give a Name to your application as LaunchDefault and package name as com.mia.launchdefault. Then click Next continously.
launchdefault com.mia.launchdefault
Click Finish
finish

Step 3 : Add a Button to activity_main.xml.

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#D8D8D8"
        android:text="Calculator" />

</LinearLayout>


Step 4 : Go to MainActivity.java from src->com.mia.launchdefault. Code for MainActivity is as follows.

MainActivity.java
package com.mia.launchdefault;

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

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  final Button buttonC = (Button)findViewById(R.id.button1); 
  buttonC.setOnClickListener(new Button.OnClickListener()
  {
    @Override
             public void onClick(View v) {
     Intent i = new Intent();
     i.setAction(Intent.ACTION_MAIN);
     i.addCategory(Intent.CATEGORY_APP_CALCULATOR);
     startActivity(i);
    }
  });
 
}
}

Step 5 : Run the Project.
Calculator will open as you will press the button.

Explanation of the code:

Explanation from MainActivity.java:

  • We will prepare an Intent object named i. Then we use setAction() to open the MAIN i.e. first page of the desired app.
  •  Then we use addCategory() function and add calculator using CATEGORY_APP_CALCULATOR.
  • We can also launch many other inbuild apps using the following categories.
category inbuilt apps functions
Stay Tuned with Made In Android

Previous Page Next Page Home

No comments:

Post a Comment

Top