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: deliliu@vip.qq.com
* Company: deli.liu@eques.com.cn
* 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...");
}
}
}