افزودن و حذف کردن میان بر اپلیکیشن در اندروید
سه شنبه 20 تیر 1396در این مقاله قصد داریم با زدن یک کلیک برنامه ی شما در اولین صفحه شما قرار بگیرد و با زدن یک دکمه ی دیگر آن برنامه که میان برش ایجاد شده است پاک شود و حذف شود این برنامه می تواند در اپلیکیشن های مورد نیازتان مورد استفاده قرار بگیرد.
ابتدا باید در قسمت androidmanifest مجوز های خود را اضافه نمایید.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
در قسمت کلاس باید قطعه کد زیر را قرار دهید:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button buttonAdd, buttonRemove;
Intent intent, intent2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonAdd = (Button)findViewById(R.id.button1);
buttonRemove = (Button)findViewById(R.id.button2);
intent = new Intent(getApplicationContext(), MainActivity.class);
intent2 = new Intent();
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AddApplicationShortcutIcon();
}
});
buttonRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RemoveApplicationShortcutIcon();
}
});
}
public void AddApplicationShortcutIcon(){
intent.setAction(Intent.ACTION_MAIN);
intent2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
intent2.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AddAppShortcutIconOnHomeScreen-Android-Examples.com");
intent2.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
intent2.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent2);
}
public void RemoveApplicationShortcutIcon(){
intent2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
intent2.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AddAppShortcutIconOnHomeScreen-Android-Examples.com");
intent2.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent2);
}
}
و در قسمت layout باید قطعه کد زیر را قرار دهید:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.addappshortcuticononhomescreen_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="136dp" android:text="Click Here to Add application shortcut icon on home screen in android" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:text="Click Here to Remove application shortcut icon on home screen in android" /> </RelativeLayout>
کد manifest به صورت زیر خواهد بود:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.addappshortcuticononhomescreen_android_examples.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <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>
خروجی به صورت زیر خواهد بود:


- Android
- 2k بازدید
- 2 تشکر