Created
January 2, 2017 08:16
-
-
Save mikkipastel/644d124cddb93d78d2b9e0f328ef6257 to your computer and use it in GitHub Desktop.
Add shortcut to your application
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.mikkipastel.test; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.mikkipastel.onemeal.fragment.MainFragment; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//add app shortcut | |
SharedPreferences prefs = getSharedPreferences("data_install", MODE_PRIVATE); | |
boolean insertBranch = prefs.getBoolean("install_status", false); | |
if (insertBranch) { | |
addShortcut(); | |
} | |
if (savedInstanceState == null) { | |
//1st created | |
//place fragment | |
getSupportFragmentManager().beginTransaction() | |
.add(R.id.contentContainer, MainFragment.newInstance()) | |
.commit(); | |
} | |
} | |
private void addShortcut() { | |
//Adding shortcut for MainActivity | |
//on Home screen | |
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); | |
shortcutIntent.setAction(Intent.ACTION_MAIN); | |
Intent addIntent = new Intent(); | |
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); | |
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test"); | |
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, | |
Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher)); | |
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); | |
getApplicationContext().sendBroadcast(addIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment