Tuesday 19 February 2019

How to navigate to the specific class on click of notification in android. | For the same add home screen in backstack

MainActivity.class:- call below method on the click of any button :-
private void addNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My Notification")
            .setContentText("This is my first notification app.My notification supriya");
    Intent notificationIntent = new Intent(this, MyNotificationDetail.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MyNotificationDetail.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());

}

NotificationDetail.class:-
public class MyNotificationDetail extends AppCompatActivity {
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
    }
}
notification.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"    
android:layout_width="fill_parent"    
android:layout_height="fill_parent" >

    <TextView        
android:layout_width="fill_parent"        
android:layout_height="400dp"        
android:text="Hi, Your Detailed notification view goes here...." />
</LinearLayout>

Manifests.xml:-
<activity    
android:name=".MyNotificationDetail"    
android:label="Detail 0f Notification"    
android:parentActivityName=".MainActivity">
    <meta-data        
android:name="android.support.PARENT_ACTIVITY"        
android:value=".MainActivity" />

</activity>

ScreenShots :-
  

Wednesday 13 February 2019

How to launch app from deeplink in Android TV and Android xiaomi TV

From deep-link or from notification when we r launching any app than after go back from that screen if we have to show home screen of application than we have to do following thing:-

1. Open Splash Screen and do the following this code is for xiaomi tv deeplink:
Intent deepLinkIntent = getIntent();
if (deepLinkIntent == null) {
    Log.d("SplashActivity", "deepLink  intent is null: ");

} else {
    Log.d("SplashActivity", "deepLink  intent getExtras: " + deepLinkIntent);
    String str = deepLinkIntent.toString();
    if (str.contains("dat=yourAppName://asset/")) {
        String s1 = "dat=yourAppName://asset/";
        int index = str.indexOf(s1);
        if (index != -1) {
            int index2 = str.indexOf(" ", index);
            deepLinkAssetId = str.substring(index + 17, index2);
            Log.d("SplashActivity", str.substring(index + 17, index2));
        }
    }
}
2. open menifest file :- 
         <activity
    android:name=".show.ui.activity.ShowDetailsActivity"
    android:theme="@style/DetailPageTheme">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".home.ui.activity.HomeActivity"/>
</activity>

 3.  Now Go the SplashScreen and write following code to navigate from respective screen :-
Intent intent = new Intent(SplashActivity.this,ShowDetailsActivity.class);
intent.putExtras(bundle);//if any bundle or data require in respective screen than needs to send the same..
TaskStackBuilder.create(SplashActivity.this).addNextIntentWithParentStack(intent).startActivities();
finish();

Monday 4 February 2019

How to use "Listener" inside Broadcast example

If we wants to update anyUI base using listener inside  Broadcast OnReceive() method than we have to do the following:-

1. Like there is a text_box in MainActivity.java where we have to set text if power connected to the phone .
2. create Listener like following
public interface ListenerBroadcast {
    void setDataFromBroadcast(String broadcastData);
}
2. Open MainActivity and take a text_box inside xml code will be like following:-
activity_main:-
Implement ListenerBroadcast to the MainActivity and override the method code will be :-
@Override
public void setDataFromBroadcast(String broadcastData) {
    dataOfBroadcast.setText("Broadcast Data = "+ broadcastData);
}
dataOfBroadcast is obj of Textview.
<TextView
    android:id="@+id/dataOfBroadcast"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
inside OnCreate():-
MyBroadcast.setDataFromBroadcast(this);
3. Add MyBroadcast class and inside onReceive() code will be following:-

public class MyBroadcast extends BroadcastReceiver {
    private static ListenerBroadcast listenerBroadcast;
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Power connected",Toast.LENGTH_LONG).show();
        if(listenerBroadcast != null ) {
            listenerBroadcast.setDataFromBroadcast("ACTION_POWER_CONNECTED");
        }
    }
    public static void setDataFromBroadcast(ListenerBroadcast listener){
        listenerBroadcast = listener;

    }
}
4. Add Broadcast to the Manifest.xml file 
<receiver android:name=".MyBroadcast">
    <intent-filter >
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    </intent-filter>
</receiver>

Friday 1 February 2019

Example to auto check Internet Connection Status in android.

1. While using  Broadcast Receiver it will always trigger when internet is not there if the  activity is closed. So for this we have to make an java class naming MyApplication.java extended by Application .This class is used to check if activity is visible or not and trigger receiver if activity is visible. The class will look like following:-

public class MyApplication extends Application {
    private static MyApplication myApplication;

    @Override    public void onCreate() {
        super.onCreate();
        Log.d("supriya","onCreate");
        myApplication = this;
    }
    public static synchronized MyApplication getInstance() {
        Log.d("supriya","synchronized MyApplication");
        return myApplication;
    }
    public void setConnectivityListener(ConnectivityReceiver
.ConnectivityReceiverListener listener) {
        Log.d("supriya","setConnectivityListener ");
        ConnectivityReceiver.connectivityReceiverListener = listener;
    }
}
2. Create BroadcastReceiver :-
public class ConnectivityReceiver extends BroadcastReceiver {
    public static ConnectivityReceiverListener connectivityReceiverListener;
    public ConnectivityReceiver() {
        super();
    }
    @Override    public void onReceive(Context context, Intent intent) {
        if(connectivityReceiverListener != null) {
            connectivityReceiverListener.onNetworkConnectionChanged(amIConnected(context));
        }
    }

    public static boolean amIConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) 
context.getApplicationContext().getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);
    }
}
3.Manifests will be like following :- 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

<application    
android:name=".MyApplication"    
android:allowBackup="true"    
android:icon="@mipmap/ic_launcher"    
android:label="@string/app_name"    
android:roundIcon="@mipmap/ic_launcher_round"    
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=".ConnectivityReceiver"        
android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
</application>
4. MainActivity.java:-
public class MainActivity extends AppCompatActivity implements ConnectivityReceiver.ConnectivityReceiverListener{
    TextView tvMsg;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMsg= (TextView) findViewById(R.id.tvMsg);
        checkConnection();
        ConnectivityReceiver.amIConnected(this);
    }

    private void checkConnection() {
        boolean isConnected = ConnectivityReceiver.amIConnected(this);
        if(isConnected) {
            tvMsg.setText("Connected");
        } else {
            tvMsg.setText("No internet connection");
        }
    }

    @Override    protected void onResume() {
        super.onResume();
        // register connection status listener        
MyApplication.getInstance().setConnectivityListener(this);
    }

    /**     * Callback will be triggered when there is change in     
* network connection     */       
public void onNetworkConnectionChanged(boolean isConnected) {
        if(isConnected) {
            tvMsg.setText("Connected");
            Toast.makeText(getApplicationContext(), "isConnected==" + isConnected, Toast.LENGTH_LONG).show();
        } else {
            tvMsg.setText("No internet connection");
            Toast.makeText(getApplicationContext(), "notConnected==" + isConnected, Toast.LENGTH_LONG).show();
        }
    }
}

How to check that app have internet connection or not in android

This post is to "Detecting internet connection status in your app"  its  very easy to do , below is the code how to do this:-

1. Open your manifests file and take following permission  :-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2. Open your class where you wants to check the internet connection and write one method like following this method will return true or false :-
private boolean amIConnected() {
    ConnectivityManager connectivityManager = (ConnectivityManager) 
getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

3. Now call this method in onCreate of class like following :-


if(amIConnected()) {
    Log.d("supriya","..user is connected"+amIConnected());
} else {
    Log.d("supriya","not connected");
}
4. Full code like following :-
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(amIConnected()) {
            Log.d("supriya","..user is connected"+amIConnected());
        } else {
            Log.d("supriya","not connected");
        }
    }

    private boolean amIConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService
(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}
Note:-
For testing i am printing the log you can also keep toast messages here