zoukankan      html  css  js  c++  java
  • 程序自动创建 “快捷图标” 问题

    <?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

  • 相关阅读:
    linux系统中只删除所有文件或只删除所有目录
    redhat7使用nmtui配置网络
    xshell,winscp,xftp无法用root用户登录
    windows无法访问指定设备路径或文件,你可能没有权限
    挂起/释放执行sap Job
    ALE,RFC和CPIC之间的关系
    CALL RFC synchronize and asychronize Examples
    关于BAPI
    ABAP调用SAP的单位转换函数进行单位转换
    BASIS--Client 锁定和解锁
  • 原文地址:https://www.cnblogs.com/firecode/p/2856008.html
Copyright © 2011-2022 走看看