- 有些開發者的Android App會透過Intent啟動相關功能之一系列選單,選單中會問你是否永久使用該第三方App,如果你錯誤選擇為永久使用,則要取消該第三方App之預設值,等到下一次透過Intent才會跳出系列選單,否則是不會跳出系列選單,本文章教你如何取消預設值,以下範例透過Intent啟動第三方Camera App,你可以自己選則該Intent啟動何類功能之第三方App
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
public class CameraActivity extends Activity {
private Button cameraBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initObject();
}
public void initObject() {
cameraBtn = (Button) this.findViewById(R.id.cameraBtn);
cameraBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
final Intent cameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE); // 設定自己功能的Intent
PackageManager packageManager = CameraActivity.this
.getPackageManager();
List<ResolveInfo> resolveInfoList = packageManager
.queryIntentActivities(cameraIntent, 0);
List<IntentFilter> intentFilterList = new ArrayList<IntentFilter>();
List<ComponentName> preferredActivities = new ArrayList<ComponentName>();
PackageInfo packageInfo = null;
packageManager.getPreferredActivities(intentFilterList,
preferredActivities, null);
for (ComponentName activity : preferredActivities) {
for (ResolveInfo rinfo : resolveInfoList) {
if (rinfo.activityInfo.applicationInfo.packageName
.equals(activity.getPackageName())) {
try {
packageInfo = packageManager.getPackageInfo(
activity.getPackageName(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
final PackageInfo currPackageInfo = packageInfo;
if (packageInfo != null) {
new AlertDialog.Builder(CameraActivity.this, 1)
.setMessage("是否取消預設值")
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
Intent showSettings = new Intent();
showSettings
.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uriAppSettings = Uri
.fromParts(
"package",
currPackageInfo.packageName,
null);
showSettings
.setData(uriAppSettings);
startActivity(showSettings);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
CameraActivity.this
.startActivity(cameraIntent);
}
}).show();
} else {
CameraActivity.this.startActivity(cameraIntent);
}
}
});
}
}
2. activity_camera.xml
<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=".CameraActivity" >
<Button
android:id="@+id/cameraBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Start Camera Intent" />
</RelativeLayout>
沒有留言:
張貼留言