update availability check
You can store the latest version of your app in Firebase realtime database, and then check and compare it with the actual version of installed app. Follow the instructions below to know how to do this.
step1:
Android manifest:
In order for our activity to access the internet checking update
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.azim" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.azim" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
setp:2
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="@+id/textview"
android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Hello world"
/> </LinearLayout>
add compent in mainActivity
package wallpapers.project.andro.istone.neo.com.nwallpapers;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriPermission;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ValueEventListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {private FirebaseDatabase Version = FirebaseDatabase.getInstance();
private DatabaseReference Ver = Version.getReference("version");
private ChildEventListener childEventListener;
private String package_name ="";
private String your_version ="";
private String latest_version = "";
private HashMap<String, Object> map = new HashMap<>();
private ArrayList<HashMap<String, Object>> map1 = new ArrayList<>();private Timer timer = new Timer();
private TimerTask _timer;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );_timer = new TimerTask() {
@Override
public void run() {
runOnUiThread( new Runnable() {
@Override
public void run() {
updata();
}
});
}
};
timer.schedule( _timer, (3500) );}private void main(){
if (new_users.getString( "new", "" ).equals( "" )) {
new_user.setAction( Intent.ACTION_VIEW );
new_user.setClass( getApplicationContext(), SingupActivity.class );
startActivity( new_user );
finish();
} else {
new_user.setAction( Intent.ACTION_VIEW );
new_user.setClass( getApplicationContext(), Wallpapers.class );
startActivity( new_user );
finish();
new_user.setAction( Intent.ACTION_VIEW );
new_user.setClass( getApplicationContext(), AdsActivity.class );
startActivity( new_user );
}
}private void updata(){
package_name = "wallpapers.project.andro.istone.neo.com.nwallpapers";
try {
android.content.pm.PackageInfo pinfo = getPackageManager().getPackageInfo(
package_name, android.content.pm.PackageManager.GET_ACTIVITIES);
your_version = pinfo.versionName;
} catch (Exception e){
Toast.makeText( this, e.toString(), Toast.LENGTH_SHORT ).show();
}
DatabaseReference rootRef = Version.getReference();
rootRef.child("version").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
} else {
map = new HashMap<>();
map.put( "v", your_version );
Ver.child( "app" ).updateChildren( map );
map.clear();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
Ver.addChildEventListener( new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Ver.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
map1 = new ArrayList<>();
try{
GenericTypeIndicator<HashMap<String,Object>> ind = new GenericTypeIndicator<HashMap<String, Object>>(){};
for (DataSnapshot data: dataSnapshot.getChildren()){
HashMap<String,Object> map = data.getValue(ind);
map1.add(map);
}
} catch (Exception e){
neoistone.message(MainActivity.this,e,null);
}
latest_version = map1.get((int)0).get("v").toString();
if(Double.parseDouble(latest_version) > Double.parseDouble(your_version)){
Intent send = new Intent();
send.setAction(Intent.ACTION_VIEW);
send.setClass(getApplicationContext(),UpdateActivity.class);
startActivity(send);
finish();
} else {
if (Double.parseDouble(your_version) > Double.parseDouble(latest_version)){
Ver.child("app").child("v").setValue(your_version);
} else {
main();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}}
Any problem Contacts me:
whatsapp: group:- Join now
youtube channal:- go to help
0 Comments: