下面列出了怎么用android.os.StrictMode.ThreadPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) {
ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
try {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyDeath()
.build());
return onBindSlice(sliceUri, new ArraySet<>(supportedSpecs));
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Retrieves the single activity that matches the given intent, or null if none found.
* @param intent The intent to query.
* @return The matching activity.
*/
public ResolveInfo resolveActivity(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().resolveActivity(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Retrieves the list of activities that can respond to the given intent.
* @param intent The intent to query.
* @return The list of activities that can respond to the intent.
*/
public List<ResolveInfo> getActivitiesThatCanRespondToIntent(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentActivities(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Retrieves the list of activities that can respond to the given intent. And returns the
* activites' meta data in ResolveInfo.
*
* @param intent The intent to query.
* @return The list of activities that can respond to the intent.
*/
public List<ResolveInfo> getActivitiesThatCanRespondToIntentWithMetaData(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentActivities(
intent, PackageManager.GET_META_DATA);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Retrieves the list of services that can respond to the given intent.
* @param intent The intent to query.
* @return The list of services that can respond to the intent.
*/
public List<ResolveInfo> getServicesThatCanRespondToIntent(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentServices(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
@Override
public void onCreate() {
super.onCreate();
setThreadPolicy(new ThreadPolicy.Builder().detectAll().penaltyLog().build());
setVmPolicy(new VmPolicy.Builder().detectAll().penaltyLog().build());
}