Friday, October 6, 2017

android - Intent for battery power 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="#ecebff"
    >
    <Button
        android:id="@+id/btn_do_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go To Battery Usage"
        android:layout_margin="25dp"
        android:layout_gravity="center"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
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) {
                /*
                    String ACTION_POWER_USAGE_SUMMARY
                        Activity Action : Show power usage information to the user.
                        Input: Nothing.
                        Output: Nothing.
                        Constant Value: "android.intent.action.POWER_USAGE_SUMMARY"
                */

                // Initialize a new intent
                Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);

                /*
                    ResolveInfo
                        Information that is returned from resolving an intent against an
                        IntentFilter. This partially corresponds to information collected from
                        the AndroidManifest.xml's <intent> tags.
                */

                /*
                    ResolveInfo resolveActivity (Intent intent,int flags)
                        Determine the best action to perform for a given Intent. This is how
                        resolveActivity(PackageManager) finds an activity if a class has not
                        been explicitly specified.

                        Note: if using an implicit Intent (without an explicit ComponentName
                              specified), be sure to consider whether to set the MATCH_DEFAULT_ONLY
                              only flag. You need to do so to resolve the activity in the same way
                              that startActivity(Intent) and Intent.resolveActivity(PackageManager) do.

                    Parameters
                        intent Intent : An intent containing all of the desired specification
                                        (action, data, type, category, and/or component).

                        flags int : Additional option flags to modify the data returned. The most
                                    important is MATCH_DEFAULT_ONLY, to limit the resolution to
                                    only those activities that support the CATEGORY_DEFAULT.

                    Returns
                        ResolveInfo Returns a ResolveInfo object containing the final activity
                        intent that was determined to be the best action. Returns null if no
                        matching activity was found. If multiple matching activities are found
                        and there is no default set, returns a ResolveInfo object containing
                        something else, such as the activity resolver.
                */

                // Initialize new resolve info
                ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);

                // If the battery app exists in the device
                if(resolveInfo != null){
                    // Start the activity to display battery power usage
                    startActivity(powerUsageIntent);
                }else {
                    Toast.makeText(mContext,"Not found.",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}