android 一些小功能实现

调用发送短信界面



    /**

     * 调用发送短信界面

     */

public void sendSms() {

       Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));

       intent.putExtra("sms_body",

              getResources().getString(R.string.smscontent));

       startActivity(intent);

    }



为程序创建桌面快捷方式

/**

     * 为程序创建桌面快捷方式

     */

private void addShortcut() {

       Intent shortcut = new Intent(

              "com.android.launcher.action.INSTALL_SHORTCUT");



       // 快捷方式的名称

       shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,

              getString(R.string.youbanradio));

       shortcut.putExtra("duplicate", false); // 不允许重复创建



       // 指定当前的Activity为快捷方式启动的对象: 如com.everest.video.VideoPlayer

       // 注意:ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序

       ComponentName comp = new ComponentName(this.getPackageName(), "."

              + this.getLocalClassName());

       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(

              Intent.ACTION_MAIN).setComponent(comp));



       // 快捷方式的图标

       ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(

              this, R.drawable.icon);

       shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);



       sendBroadcast(shortcut);

    }

在Mainfest中加入权限

<!-- 创建桌面快捷方式权限 -->

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



返回首页

    /**

     * 返回首页

     */

public void goBackMainPage() {

       Intent homeIntent = new Intent(Intent.ACTION_MAIN);

       homeIntent.addCategory(Intent.CATEGORY_HOME);

       startActivity(homeIntent);

       setNotiType(R.drawable.icon_small,

              getResources().getString(R.string.youbanradio));

    }



安装apk

// 安装apk

public void installApk(String name) {

       if (name == null || name.length() <= 0 || name.lastIndexOf(".apk") <= 0) {

           return;

       }

       String fileName = Environment.getExternalStorageDirectory()

              + FileUtil.APK_PATH + name;

       Intent intent = new Intent(Intent.ACTION_VIEW);

       intent.setDataAndType(Uri.fromFile(new File(fileName)),

              "application/vnd.android.package-archive");

       startActivity(intent);

       downloadApkName = name;

       exit();

    }


TelephonyManager tm =(TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);

       tm.listen(newTeleStateListener(), PhoneStateListener.LISTEN_CALL_STATE);



class TeleStateListener extends PhoneStateListener {



       @Override

       public void onCallStateChanged(int state, String incomingNumber) {

           // TODO Auto-generatedmethod stub

           super.onCallStateChanged(state, incomingNumber);



           AudioPlayer mPlayer = AppConst.getInstance()

                  .getAudioPlayerController().getAudioPlayer();



           switch (state) {

           case TelephonyManager.CALL_STATE_OFFHOOK:

              break;

           case TelephonyManager.CALL_STATE_IDLE:

              if (mPlayer.getStartPlayState()) {

                  if (!mPlayer.getPlayer().isPlaying() &&!mPlayer.isPause()) {

                     mPlayer.getPlayer().start();

                  }

              }

              break;

           case TelephonyManager.CALL_STATE_RINGING:

              if (mPlayer.getStartPlayState()) {

                  if (mPlayer.getPlayer().isPlaying()) {

                     mPlayer.getPlayer().pause();

                  }

              }

              break;

           }

       }



    }

你可能感兴趣的:(android)