Saturday, 24 October 2015

How to create alert dialog outside of an android application


         Here I am going to explain how to create an Alert Dialog outside of android application.For this I am using BroadcastReceiver,The BroadcastReceiver  triggers whenever the charger connected to the device.


Step 1)Test.java

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

public class Test extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Step 2)activity_main.xml

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina Popup Testing" />

</RelativeLayout>

                                                    

Step 3)Implementing the BroadcastReceiver

    When implementing a broadcast receiver you have to do two steps:

                    You have to create a subclass of Android’s BroadcastReceiver
                    You have to implement the onReceive() method

   
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BatteryReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) 
{

    Intent intent = new Intent(context, MainActivity.class);
    intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent .addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    intent .putExtra("value", "BatteryConnected");
    context.startActivity(intent );

}

}



Step 4)Registering a BroadcastReceiver in the manifest file

    <receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true" >
           <intent-filter>
            <action  android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            </intent-filter>
     </receiver>




Step 5)MainActivity.java
   
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import  android.os.Bundle;

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
  String newMessage = getIntent().getExtras().getString("value");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("New Message")
.setMessage(newMessage)
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() 
    {
public void onClick(DialogInterface dialog, int id)
              {
dialog.cancel();
finish();
              }
     })
.setNegativeButton("No", new DialogInterface.OnClickListener()
                {
      public void onClick(DialogInterface dialog, int id)
             {
dialog.cancel();
finish();
             }                    
   });
AlertDialog alert builder.create();
alert.show();
  }
}

Step 6)AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testpopup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Test"
            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=".MainActivity" >
        </activity>

        <receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
            <action  android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


Happy Coding :)