在Android中,有一个文件包含有关包的信息:" /data/system/packages.xml"。
-rw-rw ---- system system 353111 2013-01-01 02:00 packages.xml
该文件似乎不包含任何机密信息。但是,对其他用户没有读/写操作权限。
Settings(Context context, File dataDir) {
mContext = context;
mSystemDir = new File(dataDir, "system");
mSystemDir.mkdirs();
FileUtils.setPermissions(mSystemDir.toString(),
FileUtils.S_IRWXU|FileUtils.S_IRWXG
|FileUtils.S_IROTH|FileUtils.S_IXOTH,
-1, -1);
mSettingsFilename = new File(mSystemDir, "packages.xml");
mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
mPackageListFilename = new File(mSystemDir, "packages.list");
FileUtils.setPermissions(mPackageListFilename, 0660, SYSTEM_UID, PACKAGE_INFO_GID);
// Deprecated: Needed for migration
mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
}
同时我实施了一个简单的Android活动。它只是获取有关所有已安装软件包的信息。
package com.example.com.example.myfirst_epic_app;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
//my
import android.content.pm.*;
import android.content.pm.PackageManager.NameNotFoundException;
import java.util.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
/** Called when the user clicks the Send button */
public void buttonClick(View view) {
// Do something in response to button
TextView textView = (TextView) findViewById(R.id.textView1);
try {
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_SIGNATURES);
textView.setText("Found pack");
Log.i("Hello!!!", "Message after pack was found");
for (PackageInfo cur_pack : packs) {
Log.i("Hello!!!", "Name="+(cur_pack.packageName));
for (Signature sig : cur_pack.signatures) {
Log.i(""+cur_pack.packageName, "Signature : " + sig.toCharsString());
}
}
} catch (NameNotFoundException e) {
textView.setText("Not found");
}
}
}
这个简单的应用程序具有任何权限,并由调试密钥签名。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.example.myfirst_epic_app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.com.example.myfirst_epic_app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我下载了&#34; /data/system/packages.xml"来自设备的文件(我有一个root的Android图片)。 因此,我的应用程序读取并打印了关于所有软件包的LOG信息,这些软件包都安装在我的设备上,没有任何问题。
问题:
1)很明显,没有人必须写入文件packages.xml。但是,如果每个人都可以从&#34; packages.xml&#34;通过Android API为什么Android开发者没有为其他用户添加读取权限?例如:
-rw-rw-r-- system system 353111 2013-01-01 02:00 packages.xml
2)另外,我想知道如何在没有与Android API交互的情况下从本机程序中读取packages.xml中的信息?
非常感谢任何帮助。