1. 程式人生 > >Android-AndroidManifest.xml預設啟動的Activity(探索篇01)

Android-AndroidManifest.xml預設啟動的Activity(探索篇01)

AndroidManifest.xml-->預設啟動  MusicBrowserActivity

     <activity android:name=".MusicBrowserActivity"
                  android:theme="@android:style/Theme.NoTitleBar"
                  android:exported="true"> <!-- android:exported="true" 是否支援其它應用呼叫當前元件。
                                            預設值:如果包含有intent-filter 預設值為true; 沒有intent-filter預設值為false。 
--> <intent-filter> <!-- 程式主入口 --> <action android:name="android.intent.action.MAIN" /> <!-- 啟動的Launcher --> <category android:name="android.intent.category.LAUNCHER" /> <!--
給其他地方/程式呼叫的action --> <action android:name="android.intent.action.MUSIC_PLAYER" /> <!-- 隱式啟動需要增加category.DEFAULT,new Intent()... --> <category android:name="android.intent.category.DEFAULT" /> <!-- 和ACTION_MAIN一起使用,用來啟動音樂應用程式
--> <category android:name="android.intent.category.APP_MUSIC" /> </intent-filter> </activity>

 

MusicBrowserActivity

package liudeli.music2;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.util.Log;

import liudeli.music2.service.IMediaPlaybackService;
import liudeli.music2.service.MediaPlaybackService;
import liudeli.music2.util.MusicUtils;

/**
 * Description:主頁面入口
 * Created by Liudeli on 2017/10/27.
 * About: Liudeli
 * QICQ: 1830140442
 * Email: [email protected]
 * Company: [email protected]
 * Phone: 15221634614
 * Date: 2017/10/27
 * CSDN: http://blog.csdn.net/u011967006
 */

public class MusicBrowserActivity extends Activity implements MusicUtils.Defs {

    private static final String TAG  = "MusicBrowserActivity";

    private MusicUtils.ServiceToken mToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 系統6.0許可權問題
        // requestReadExternalPermission();

        // 通過SharedPreferences get上一次tab的記錄
        int activeTab = MusicUtils.getIntPref(this, "activetab", R.id.artisttab);

        // 如果不等於所有tab選項,就預設[音樂人tab]
        if (activeTab != R.id.artisttab
                && activeTab != R.id.albumtab
                && activeTab != R.id.songtab
                && activeTab != R.id.playlisttab) {
            activeTab = R.id.artisttab;
        }

        // 去指定start那個Activity [音樂人][專輯][歌曲][播放列表]
        MusicUtils.activateTab(this, activeTab);

        // 我的理解是其他應用程式,來傳值進行邏輯操作
        String shuf = getIntent().getStringExtra("autoshuffle");
        Log.d(TAG, "shuf:" + shuf);

        if ("true".equals(shuf)) {
            mToken = MusicUtils.bindToService(this, autoshuffle);
        }
    }

    private ServiceConnection autoshuffle = new ServiceConnection() {

        public void onServiceConnected(ComponentName classname, IBinder obj) {
            // 我們需要再次繫結,這樣就可以解除繫結
            Log.d(TAG, "我們需要再次繫結,這樣就可以解除繫結");

            try {
                unbindService(this);
            } catch (IllegalArgumentException e) {
            }
            IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
            if (serv != null) {
                try {
                    serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
                } catch (RemoteException ex) {
                }
            }
        }

        public void onServiceDisconnected(ComponentName classname) {
        }
    };

    @Override
    protected void onDestroy() {
        if (null != mToken) {
            MusicUtils.unbindFromService(mToken);
        }
        super.onDestroy();
    }

    /**
     * 系統6.0許可權問題
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.d(TAG, "requestCode=" + requestCode + "; --->" + permissions.toString()
                + "; grantResult=" + grantResults.toString());
        switch (requestCode) {
            case 0: {

                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted
                    // request successfully, handle you transactions

                    // requestReadExternalPermission();
                } else {

                    // permission denied
                    // request failed

                    // requestReadExternalPermission();
                }

                return;
            }
            default:
                break;

        }
    }

    /**
     * 系統6.0許可權問題
     */
    @SuppressLint("NewApi")
    private void requestReadExternalPermission() {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "READ permission IS NOT granted...");

            if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {

                Log.d(TAG, "11111111111111");
            } else {
                // 0 是自己定義的請求coude
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
                Log.d(TAG, "222222222222");
            }
        } else {
            Log.d(TAG, "READ permission is granted...");
        }
    }

}