1. 程式人生 > >Android 系統自帶隱式intent應用

Android 系統自帶隱式intent應用

完成效果:撥打電話,傳送簡訊,播放音樂,播放視訊,開啟網頁,檢視圖片,通知欄。
PopWindow中完成效果:拍照片,從相簿選擇照片並且可裁減。
首先在清單檔案中加入如下許可權:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


主佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.jreduch07.IntentActivity">
    <com.example.administrator.jreduch07.RoundImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/riv"
        android:src="@mipmap/ruzixiaoge"
        android:layout_below="@+id/bt8"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:text="撥打電話"

        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt2"
        android:text="傳送簡訊"
        android:layout_below="@+id/bt1"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt3"
        android:layout_below="@id/bt2"
        android:text="開啟網頁"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt4"
        android:layout_below="@+id/bt3"
        android:text="播放音樂"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt5"
        android:layout_below="@id/bt4"
        android:text="播放視訊"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt6"
        android:layout_below="@id/bt5"
        android:text="檢視圖片"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt7"
        android:layout_below="@+id/bt6"
        android:text="安裝APK"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通知欄訊息"
        android:id="@+id/bt8"
        android:layout_below="@+id/bt7"
        />
</RelativeLayout>

佈局效果:

PopWindow佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.jreduch07.IntentActivity">
    <com.example.administrator.jreduch07.RoundImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/riv"
        android:src="@mipmap/ruzixiaoge"
        android:layout_below="@+id/bt8"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:text="撥打電話"

        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt2"
        android:text="傳送簡訊"
        android:layout_below="@+id/bt1"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt3"
        android:layout_below="@id/bt2"
        android:text="開啟網頁"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt4"
        android:layout_below="@+id/bt3"
        android:text="播放音樂"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt5"
        android:layout_below="@id/bt4"
        android:text="播放視訊"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt6"
        android:layout_below="@id/bt5"
        android:text="檢視圖片"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt7"
        android:layout_below="@+id/bt6"
        android:text="安裝APK"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通知欄訊息"
        android:id="@+id/bt8"
        android:layout_below="@+id/bt7"
        />
</RelativeLayout>


PopWindow動畫效果程式碼:
進入動畫程式碼:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
    <alpha
        android:duration="1500"
        android:fromAlpha="1"
        android:toAlpha="0"
        />
</set>

退出動畫程式碼:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
    <alpha
        android:duration="1500"
        android:fromAlpha="1"
        android:toAlpha="0"
        />
</set>


程式碼:
package com.example.administrator.jreduch07;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import java.io.File;

public class IntentActivity extends AppCompatActivity implements View.OnClickListener {
     private View view;
     private RoundImageView riv;
    private PopupWindow pw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        view=getLayoutInflater().inflate(R.layout.activity_pop_window,null);
        Button bt1 = (Button) findViewById(R.id.bt1);
        Button bt2 = (Button) findViewById(R.id.bt2);
        Button bt3 = (Button) findViewById(R.id.bt3);
        Button bt4 = (Button) findViewById(R.id.bt4);
        Button bt5 = (Button) findViewById(R.id.bt5);
        Button bt6 = (Button) findViewById(R.id.bt6);
        Button bt7 = (Button) findViewById(R.id.bt7);
        Button bt8 = (Button) findViewById(R.id.bt8);
        riv = (RoundImageView) findViewById(R.id.riv);
        Button bt_take=(Button)view.findViewById(R.id.bt_take);
        Button bt_selcte=(Button)view.findViewById(R.id.bt_selcte);
        Button bt_cancel=(Button)view.findViewById(R.id.bt_cancel);
        pw=new PopupWindow(view);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        riv.setOnClickListener(this);
        bt_take.setOnClickListener(this);
        bt_selcte.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
    }

    public void onClick(View v) {
        int id = v.getId();

        pw.setFocusable(true);
        ColorDrawable cd =new ColorDrawable();
        pw.setBackgroundDrawable(cd);
        backgroundAlpha(0.3f);
        pw.setAnimationStyle(R.style.pop);

        switch (id) {
            case R.id.bt1:
               //隱式Intent 由android系統幫助匹配
                //匹配規則,清單檔案中的 Intent_filter 標籤中的action
                Uri uri = Uri.parse("tel:10086");
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);
                break;
            case  R.id.bt2:
                Intent it=new Intent(Intent.ACTION_VIEW);
                it.putExtra("sms_body","你好你好");
                it.setType("vnd.android-dir/mms-sms");
                startActivity(it);
                break;
            case R.id.bt3:
                Uri uri1=Uri.parse("http://www.baidu.com");
                Intent it1=new Intent(Intent.ACTION_VIEW,uri1);
                startActivity(it1);
                break;
            case  R.id.bt4:
                Intent it2 =new Intent(Intent.ACTION_VIEW);
                File file =new File("/storage/emulated/0/RM/res/song/fengniao/fengniao.mp3");
                it2.setDataAndType(Uri.fromFile(file), "audio/*");
                startActivity(it2);
                break;
            case R.id.bt5:
                Intent it3 =new Intent(Intent.ACTION_VIEW);
                File file1=new File("");
                it3.setDataAndType(Uri.fromFile(file1),"video/*");
                startActivity(it3);
                break;
            case R.id.bt6:
                Intent it4 =new Intent(Intent.ACTION_VIEW);
                File file2=new File("/storage/emulated/0/Pictures/Screenshots/Screenshot_2016-08-09-15-15-03.png");
                it4.setDataAndType(Uri.fromFile(file2),"image/*");
                startActivity(it4);
                break;
            case R.id.bt7:
                Intent it5 =new Intent(Intent.ACTION_VIEW);
                it5.setDataAndType(Uri.parse(""),
                        "");
                startActivity(it5);
                break;
            case R.id.bt8:
                notification();
                break;
            case R.id.riv:
                int weight=getResources().getDisplayMetrics().widthPixels;
                pw.setWidth(weight);
                int height =getResources().getDisplayMetrics().heightPixels/4;
                pw.setHeight(height);
                pw.showAtLocation(riv, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
                pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams ll =
                                getWindow().getAttributes();
                        ll.alpha = 1f;
                        getWindow().setAttributes(ll);
                        //設定View可見
                    }
                });
                break;

            case R.id.bt_take:
                takePhoto();
                break;
            case R.id.bt_selcte:
                phonePhoto();
                break;
            case R.id.bt_cancel:
                pw.dismiss();
                break;
        }
    }
    //訊息欄通知
    public void notification(){
        //先定義一個Intent
        Intent intent =new Intent(this,SecondActivity.class);
        PendingIntent pi=PendingIntent.getActivities(this, 0, new Intent[]{intent},
                PendingIntent.FLAG_UPDATE_CURRENT);
        //獲取通知服務
        NotificationManager nm= (NotificationManager)
                getSystemService(Activity .NOTIFICATION_SERVICE);
        Notification notification=new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("")
                .setContentInfo("我是通知欄資訊")
                .setContentTitle("奧運會")
                .setContentText("pendingInt的使用方法")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pi)
                .build();
        //通過通知服務顯示通知。
        nm.notify(0, notification);
    }

    public void backgroundAlpha(float bgAlpha){
        WindowManager.LayoutParams lp=
                getWindow().getAttributes();
        lp.alpha=bgAlpha;
        getWindow().setAttributes(lp);
    }

    private String capturePath="";

    //呼叫相機
    public void takePhoto(){
      Intent camera =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       File parent=FileUitlity.getInstance(getApplicationContext())
               .makeDir("head_img");
        capturePath=parent.getPath()
                +File.separatorChar
                +System.currentTimeMillis()
                +".jpg";
        camera.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(capturePath)));
        camera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(camera, 1);
    }

    public void startPicZoom(Uri uri){
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        intent.putExtra("crop","true");
        //允許裁剪
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);
        //設定裁剪比例
        //設定圖片高度寬度
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent,3);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode !=Activity.RESULT_OK){

            return;
        }
        //相機返回結果,呼叫系統裁剪
        if(requestCode==1){
           startPicZoom(Uri.fromFile(new File(capturePath)));
            //相簿返回結果,呼叫系統裁剪
        }else if(requestCode==2){
            Cursor cursor=getContentResolver().
                    query(data.getData(), new String[]{
                            MediaStore.Images.Media.DATA},null,null,null);
            cursor.moveToFirst();
            String capturePath=cursor.getString(cursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            cursor.close();
            startPicZoom(Uri.fromFile(new File(capturePath)));
        }else if(requestCode==3){
          Bundle bundle= data.getExtras();
            if(bundle!=null){
              Bitmap bitmap= bundle.getParcelable("data");
                riv.setImageBitmap(bitmap);
            }
        }
    }
    //呼叫相簿
    public void phonePhoto(){
        Intent intent=new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,2);
    }
}
效果展示: