Android Program : Create an Android application to demonstrates how to use a service to download a file from the Internet on click of Download Button.

 

Create an Android application to demonstrates how to use a service to download a file from the Internet on click of Download Button. Once done, the service notifies the activity via a broadcast receiver that the download is complete.

Soln :


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

    xmlns:tools="https://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.journaldev.broadcastreceiver.MainActivity">


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button"

        android:text="Send Broadcast"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>



MainActivity.java


package com.journaldev.broadcastreceiver;


import android.content.Intent;

import android.content.IntentFilter;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;


import butterknife.ButterKnife;

import butterknife.InjectView;

import butterknife.OnClick;


public class MainActivity extends AppCompatActivity {

    ConnectionReceiver receiver;

    IntentFilter intentFilter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        ButterKnife.inject(this);


        receiver = new ConnectionReceiver();

        intentFilter = new IntentFilter("com.journaldev.broadcastreceiver.SOME_ACTION");


    }


    @Override

    protected void onResume() {

        super.onResume();

        registerReceiver(receiver, intentFilter);

        registerReceiver(broadcastReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));


    }


@Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }

    @Override

    protected void onDestroy() {

        super.onDestroy();


        unregisterReceiver(receiver);

    }


    @OnClick(R.id.button)

    void someMethod() {


        Intent intent = new Intent("com.journaldev.broadcastreceiver.SOME_ACTION");

        sendBroadcast(intent);

    }

}



AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".ConnectionReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

    </application>
</manifest>


 ConnectionReceiver.java


public class ConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("API123",""+intent.getAction());

        if(intent.getAction().equals("com.journaldev.broadcastreceiver.SOME_ACTION"))
            Toast.makeText(context, "SOME_ACTION is received", Toast.LENGTH_LONG).show();

        else {
            ConnectivityManager cm =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null &&
                    activeNetwork.isConnectedOrConnecting();
            if (isConnected) {
                try {
                    Toast.makeText(context, "Network is connected", Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(context, "Network is changed or reconnected", Toast.LENGTH_LONG).show();
            }
        }
    }

}




Post a Comment

0 Comments