271.添加快捷圖標

添加桌面的快捷圖標,

需要增加權限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

actvity_main.xml

<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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

package com.example;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Intent intent = new Intent();
		intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		
		/**
		 * 1  幹什麼事情
		 * 2  叫什麼名字
		 * 3  圖標長什麼樣子
		 */
		Intent dowhtIntent = new Intent();
		//告訴系統打電話意圖
		dowhtIntent.setAction(Intent.ACTION_CALL);
		//給誰打電話
		dowhtIntent.setData(Uri.parse("tel://110"));
		//叫什麼名字
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "圖標");
		//圖標的樣子
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, 
				BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
		
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, dowhtIntent);
		sendBroadcast(intent);
	}

	@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;
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章