Friday, October 6, 2017

android - Intent for data usage summary

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/base_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#afc2b5"
    >
    <Button
        android:id="@+id/btn_do_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go To Data Usage"
        android:layout_margin="25dp"
        android:layout_gravity="center"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mButton = findViewById(R.id.btn_do_task);

        // Set a click listener gor button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    // Initialize a new intent
                    Intent intent = new Intent(Intent.ACTION_MAIN);

                    /*
                        Intent setComponent (ComponentName component)
                            (Usually optional) Explicitly set the component to handle the intent.
                            If left with the default value of null, the system will determine the
                            appropriate class to use based on the other fields (action, data, type,
                            categories) in the Intent. If this class is defined, the specified class
                            will always be used regardless of the other fields. You should only set
                            this value when you know you absolutely want a specific class to be
                            used; otherwise it is better to let the system find the appropriate
                            class so that you will respect the installed applications and
                            user preferences.

                        Parameters
                            component ComponentName : The name of the application component to
                                                      handle the intent, or null to let the system
                                                      find one for you.
                        Returns
                            Intent : Returns the same Intent object, for chaining multiple calls
                                     into a single statement.
                                     This value will never be null.
                    */
                    intent.setComponent(
                            new ComponentName(
                            "com.android.settings",
                            "com.android.settings.Settings$DataUsageSummaryActivity"
                            )
                    );

                    /*
                        FLAG_ACTIVITY_NEW_TASK
                            If set, this activity will become the start of a new task on this
                            history stack.

                        FLAG_ACTIVITY_CLEAR_TOP
                            If set, and the activity being launched is already running in the
                            current task, then instead of launching a new instance of that activity,
                            all of the other activities on top of it will be closed and this Intent
                            will be delivered to the (now on top) old activity as a new Intent.

                    */
                    // Set the intent flags
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    // Finally, start the activity to view data usage report
                    startActivity(intent);
                }catch (ActivityNotFoundException e){
                    Toast.makeText(mContext,"Not found.",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}