So its time to add an in-build updater to your application , so as i did in previous tutorial this post will be also a continuation of the Android Studio Tutorials .
We are doing Android Studio projects as a continuation , so am just moving on to the main part . I will explain the basic things here as post and rest you can refer to my youtube video .
Watch The Video And Follow Me :
We are doing Android Studio projects as a continuation , so am just moving on to the main part . I will explain the basic things here as post and rest you can refer to my youtube video .
Watch The Video And Follow Me :
- Start a new project or you can add it in an existing project .
- Choose Application name , domain name , package name ...
- And i am doing this project on Navigation Drawer Activity .
- After gradle building and all follow the bellow steps .
- Download volley.jar file from the download options or click here .
- Now we have to add it to our project .
- Choose Project instead of Android .
- Open your project -> app -> libs .
- Now copy the downloaded file volley.jar and paste it on the libs file .
- Now after pasting left click and select the option Add as Library .
- Now the gradle will build and after that you need to go back to Android .
- Go to AndroidManifest.xml .
- Select the entire codes and replace it with this and be sure that you don't change your package name for me its com.appname.app .
- Now go to java part .
- Select your package name , left click and select New -> Java Class .
- Name the class UpdateService .
- Select the entire code except the package name and replace it with this .
- Here you need a github account and create a github page .
- For that you need to make a repository with the same name of that of your user name that you used in the github account .
- Now you must create a page with content just number "1" .
- Now copy the link of the page and replace my github page link https://vishnutechmobs.github.io/update-link.html with yours .
- When you change this number "1" to any link "2" update box appears .
- And also the content inside " boolean resp = response.contains("1"); " this line which is 1 here must be same as that of the github page content .
- Then by changing the github page content "1" to "2" etc you can announce update .
- In short it works in a synchronous manner . If any one of the content is different then update box comes .
- An important note : don't forget to change the value "1" on the UpdateService to the change value on your updated application . If you have any doubt feel free to contact me .
- Now create another java class . New -> Java Class .
- Name it UpdateDialog .
- Select the entire code except the package name and replace it with this .
- Now go to MainActivity .
- Select the entire code except the package name and replace it with this .
- Thats it your application with In-Build Updater is ready to be build .
- Go to build option and choose option Build APK .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.appname.app" > | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.READ_LOGS" /> | |
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |
<application | |
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" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme.NoActionBar" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".UpdateDialog" | |
android:theme="@style/Animation.AppCompat.Dialog"/> | |
<service | |
android:name=".UpdateService" | |
android:enabled="true" | |
android:exported="true"/> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.appname.app; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
// Copyright owner vishnu | |
// Official website : www.tipsandtricksonandroid.blogspot.com | |
// Created on : 26-april-2017 // | |
public class UpdateService extends Service { | |
public UpdateService() { | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); | |
String url = "https://vishnutechmobs.github.io/update-link.html"; | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
if (response != null) { | |
boolean resp = response.contains("1"); | |
if (!resp) { | |
//New Update Available | |
Intent intent1 = new Intent(UpdateService.this,UpdateDialog.class); | |
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent1); | |
} else { | |
Toast.makeText(UpdateService.this, "Running Current Version", Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
volleyError.printStackTrace(); | |
} | |
}); | |
queue.add(stringRequest); | |
return Service.START_NOT_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO: Return the communication channel to the service. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.appname.app; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
// Copyright owner vishnu | |
// Official website : www.tipsandtricksonandroid.blogspot.com | |
// Created on : 26-april-2017 // | |
public class UpdateDialog extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState){ | |
super.onCreate(savedInstanceState); | |
final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); | |
alertDialog.setTitle("Update Available"); | |
alertDialog.setMessage("A new version of this application is available ."+"\nWe have improved a lot from previous version"); | |
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Download", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setData(Uri.parse("https://vishnutechmobs.github.io/update.html")); | |
if(intent.resolveActivity(getPackageManager()) !=null){ | |
startActivity(intent); | |
} | |
} | |
}); | |
alertDialog.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.appname.app; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.view.View; | |
import android.support.design.widget.NavigationView; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends AppCompatActivity | |
implements NavigationView.OnNavigationItemSelectedListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
//*-------------------Tech Mobs update activity------------------*// | |
Intent intent = new Intent(MainActivity.this, UpdateService.class); | |
startService(intent); | |
//*-------------------Tech Mobs update activity end------------------*// | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} | |
}); | |
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); | |
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( | |
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); | |
drawer.setDrawerListener(toggle); | |
toggle.syncState(); | |
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); | |
navigationView.setNavigationItemSelectedListener(this); | |
} | |
@Override | |
public void onBackPressed() { | |
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); | |
if (drawer.isDrawerOpen(GravityCompat.START)) { | |
drawer.closeDrawer(GravityCompat.START); | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@SuppressWarnings("StatementWithEmptyBody") | |
@Override | |
public boolean onNavigationItemSelected(MenuItem item) { | |
// Handle navigation view item clicks here. | |
int id = item.getItemId(); | |
if (id == R.id.nav_camera) { | |
// Handle the camera action | |
} else if (id == R.id.nav_gallery) { | |
} else if (id == R.id.nav_slideshow) { | |
} else if (id == R.id.nav_manage) { | |
} else if (id == R.id.nav_share) { | |
} else if (id == R.id.nav_send) { | |
} | |
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); | |
drawer.closeDrawer(GravityCompat.START); | |
return true; | |
} | |
} |
Part 1 - Build An Application For Your Website ( Link To The Post - Click Here )
Post A Comment:
0 comments: