我正在开发一个锁定特定应用程序的 AppLock。为此,我有一个包含 ListView 的屏幕,并且 ListView 有多个开关,每个安装的应用程序一个。
我需要知道用户打开了哪些开关并提取其中写入的文本。
SensitiveApps.java
package com.sr.droidlock;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SensitiveApps extends ActionBarActivity {
Switch appName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensitive_apps);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
ArrayList<String> tasks = new ArrayList<String>();
appName = (Switch) findViewById(R.id.appName) ;
for (Object object : pkgAppsList)
{
ResolveInfo info = (ResolveInfo) object;
final String title = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
tasks.add(title);
}
Collections.sort(tasks, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
ArrayAdapter<String> adapter = new ArrayAdapter <String>(this,R.layout.layout_sensitive_apps, R.id.appName ,tasks);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
activity_sensitive_apps.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.sr.droidlock.SensitiveApps">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
layout_sensitive_apps
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<Switch
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="New Switch"
android:id="@+id/appName" />
</LinearLayout>