程序自动创建 “快捷图标” 问题

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tangly.shortcut"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".StartActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name="LoginActivity"/>
    </application>
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
</manifest>

 

 

import java.util.Timer;
import java.util.TimerTask;




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author tanghl
 * 载入界面图片
 *
 */
public class StartActivity extends Activity{

    private static final String TAG = "StartActivity";
    private static final String SHARE_SHORT_CUTS="SHARE_SHORT_CUTS";
    private static final String ISCREATED="iscreated";
    private static final String ACTION_INSTALL_SHORTCUT ="com.android.launcher.action.INSTALL_SHORTCUT";   
    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";  
    
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);//禁用横屏
        requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      
       
//       if(!loadSharePref()){
           addShortcut();
            try{
                setContentView(R.layout.start_layout);
                final Timer timer = new Timer();
                TimerTask task = new TimerTask() {
                    public void run() {
                        Intent intent = new Intent();
                        intent.setClass(StartActivity.this, app.class);
                        startActivity(intent);
                        timer.cancel();
                        StartActivity.this.finish();
                    }
                };
                //timer.schedule(task, 2000);
                timer.schedule(task, 1000);
            }catch(Exception e){
                Log.e(TAG, e.toString());
            }
//       }else{
//           Intent intent = getIntent();
//           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
//           intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);   
//           intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
//           startActivity(intent);
//       }
//            this.finish();

    }
    
private void addShortcut() {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
    shortcut.putExtra("duplicate", false);    // 不允许重复创建
    String action = "com.android.action.test";
    Intent intent = new Intent(this, this.getClass());
//    intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.setAction(action);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); //设置程序图标
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
    
    sendBroadcast(shortcut);
    saveSharePref();
}
    
    
    
/*
 * 创建快捷方式后,share保存标识
 */
    private void saveSharePref() {
        SharedPreferences share = getSharedPreferences(SHARE_SHORT_CUTS, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
        share.edit().putString(ISCREATED,"YES").commit();
    }
    
    /*
     * 检查之前是否创建过 快捷方式
     */
    private boolean loadSharePref(){
        boolean flag = false;
        SharedPreferences share = getSharedPreferences(SHARE_SHORT_CUTS, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
        String result = share.getString(ISCREATED, "");
        if(null != result && "YES".equals(result)){
            flag = true;
        }
        return flag;
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "startActivity is running .....");
        super.onResume();
    }

}

 

 

下载地址  下载1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值