如何为android开发应用程序锁

时间:2014-09-11 22:53:03

标签: android service

我正在尝试为Android开发应用程序锁。我还编写了使用服务的类,但随后出现错误。这样说:

Logcat结果:

 09-12 06:42:38.745: D/AbsListView(23708): Get MotionRecognitionManager
09-12 06:42:39.775: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:39.775: W/PackageManager(23708): Failure retrieving resources forcom.sec.android.providers.tasks: Resource ID #0x7f020000
09-12 06:42:39.955: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:39.955: W/PackageManager(23708): Failure retrieving resources forcom.android.providers.telephony: Resource ID #0x7f020000
09-12 06:42:40.455: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:40.455: W/PackageManager(23708): Failure retrieving resources forcom.sec.android.provider.logsprovider: Resource ID #0x7f020000
09-12 06:42:41.165: W/ResourceType(23708): Failure getting entry for 0x7f060000 (t=5 e=0) in package 0 (error -75)
09-12 06:42:41.165: W/PackageManager(23708): Failure retrieving text 0x7f060000 in package com.android.keyguard
09-12 06:42:41.165: W/PackageManager(23708): android.content.res.Resources$NotFoundException: String resource ID #0x7f060000
09-12 06:42:41.165: W/PackageManager(23708):    at android.content.res.Resources.getText(Resources.java:1382)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:1198)
09-12 06:42:41.165: W/PackageManager(23708):    at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:135)
09-12 06:42:41.165: W/PackageManager(23708):    at com.the.voiceapplock.MainActivity.onCreate(MainActivity.java:41)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.Activity.performCreate(Activity.java:5541)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
09-12 06:42:41.165: W/PackageManager(23708):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 06:42:41.165: W/PackageManager(23708):    at android.os.Looper.loop(Looper.java:146)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.main(ActivityThread.java:5653)
09-12 06:42:41.165: W/PackageManager(23708):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 06:42:41.165: W/PackageManager(23708):    at java.lang.reflect.Method.invoke(Method.java:515)
09-12 06:42:41.165: W/PackageManager(23708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
09-12 06:42:41.165: W/PackageManager(23708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
09-12 06:42:41.165: W/PackageManager(23708):    at dalvik.system.NativeStart.main(Native Method)
09-12 06:42:41.165: W/ResourceType(23708): Failure getting entry for 0x7f060000 (t=5 e=0) in package 0 (error -75)
09-12 06:42:41.165: W/PackageManager(23708): Failure retrieving text 0x7f060000 in package com.android.keyguard
09-12 06:42:41.165: W/PackageManager(23708): android.content.res.Resources$NotFoundException: String resource ID #0x7f060000
09-12 06:42:41.165: W/PackageManager(23708):    at android.content.res.Resources.getText(Resources.java:1382)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:1198)
09-12 06:42:41.165: W/PackageManager(23708):    at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:135)
09-12 06:42:41.165: W/PackageManager(23708):    at com.the.voiceapplock.MainActivity.onCreate(MainActivity.java:44)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.Activity.performCreate(Activity.java:5541)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
09-12 06:42:41.165: W/PackageManager(23708):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 06:42:41.165: W/PackageManager(23708):    at android.os.Looper.loop(Looper.java:146)
09-12 06:42:41.165: W/PackageManager(23708):    at android.app.ActivityThread.main(ActivityThread.java:5653)
09-12 06:42:41.165: W/PackageManager(23708):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 06:42:41.165: W/PackageManager(23708):    at java.lang.reflect.Method.invoke(Method.java:515)
09-12 06:42:41.165: W/PackageManager(23708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
09-12 06:42:41.165: W/PackageManager(23708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
09-12 06:42:41.165: W/PackageManager(23708):    at dalvik.system.NativeStart.main(Native Method)
09-12 06:42:41.455: I/dalvikvm-heap(23708): Grow heap (frag case) to 31.920MB for 5676496-byte allocation
09-12 06:42:41.905: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:41.905: W/PackageManager(23708): Failure retrieving resources forcom.sec.android.provider.badge: Resource ID #0x7f020000
09-12 06:42:42.035: D/dalvikvm(23708): GC_FOR_ALLOC freed 6242K, 31% free 22150K/31740K, paused 28ms, total 28ms
09-12 06:42:42.045: I/dalvikvm-heap(23708): Grow heap (frag case) to 30.897MB for 4194320-byte allocation
09-12 06:42:42.455: W/ResourceType(23708): Failure getting entry for 0x7f02001e (t=1 e=30) in package 0 (error -75)
09-12 06:42:42.455: W/PackageManager(23708): Failure retrieving resources forcom.samsung.shareshot: Resource ID #0x7f02001e
09-12 06:42:42.665: D/dalvikvm(23708): GC_FOR_ALLOC freed 1116K, 25% free 27178K/35840K, paused 19ms, total 20ms
09-12 06:42:42.745: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:42.745: W/PackageManager(23708): Failure retrieving resources forcom.sec.android.providers.security: Resource ID #0x7f020000
09-12 06:42:43.425: D/dalvikvm(23708): GC_FOR_ALLOC freed 1749K, 19% free 30131K/36960K, paused 26ms, total 26ms
09-12 06:42:44.025: D/dalvikvm(23708): GC_FOR_ALLOC freed 1800K, 18% free 32527K/39316K, paused 23ms, total 23ms
09-12 06:42:44.245: W/ResourceType(23708): Failure getting entry for 0x7f020000 (t=1 e=0) in package 0 (error -75)
09-12 06:42:44.245: W/PackageManager(23708): Failure retrieving resources forcom.sec.android.Kies: Resource ID #0x7f020000
09-12 06:42:44.525: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.535: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.555: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.565: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.575: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.585: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.595: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.605: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.615: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.635: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.645: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.655: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.665: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:44.795: I/Adreno-EGL(23708): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
09-12 06:42:44.795: I/Adreno-EGL(23708): OpenGL ES Shader Compiler Version: E031.24.00.08
09-12 06:42:44.795: I/Adreno-EGL(23708): Build Date: 03/21/14 Fri
09-12 06:42:44.795: I/Adreno-EGL(23708): Local Branch: AU200+patches_03212014
09-12 06:42:44.795: I/Adreno-EGL(23708): Remote Branch: 
09-12 06:42:44.795: I/Adreno-EGL(23708): Local Patches: 
09-12 06:42:44.795: I/Adreno-EGL(23708): Reconstruct Branch: 
09-12 06:42:44.905: D/OpenGLRenderer(23708): Enabling debug mode 0
09-12 06:42:45.055: I/Choreographer(23708): Skipped 30 frames!  The application may be doing too much work on its main thread.
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.285: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.295: D/TextLayoutCache(23708): Enable myanmar Zawgyi converter
09-12 06:42:45.385: D/AndroidRuntime(23708): Shutting down VM
09-12 06:42:45.385: W/dalvikvm(23708): threadid=1: thread exiting with uncaught exception (group=0x417e2da0)
09-12 06:42:45.395: E/AndroidRuntime(23708): FATAL EXCEPTION: main
09-12 06:42:45.395: E/AndroidRuntime(23708): Process: com.the.voiceapplock, PID: 23708
09-12 06:42:45.395: E/AndroidRuntime(23708): java.lang.RuntimeException: Unable to instantiate receiver com.the.voiceapplock.StartUpReceiver: java.lang.ClassNotFoundException: Didn't find class "com.the.voiceapplock.StartUpReceiver" on path: DexPathList[[zip file "/data/app/com.the.voiceapplock-188.apk"],nativeLibraryDirectories=[/data/app-lib/com.the.voiceapplock-188, /vendor/lib, /system/lib]]
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2619)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.app.ActivityThread.access$1800(ActivityThread.java:172)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.os.Looper.loop(Looper.java:146)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.app.ActivityThread.main(ActivityThread.java:5653)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invoke(Method.java:515)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at dalvik.system.NativeStart.main(Native Method)
09-12 06:42:45.395: E/AndroidRuntime(23708): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.the.voiceapplock.StartUpReceiver" on path: DexPathList[[zip file "/data/app/com.the.voiceapplock-188.apk"],nativeLibraryDirectories=[/data/app-lib/com.the.voiceapplock-188, /vendor/lib, /system/lib]]
09-12 06:42:45.395: E/AndroidRuntime(23708):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-12 06:42:45.395: E/AndroidRuntime(23708):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2614)
09-12 06:42:45.395: E/AndroidRuntime(23708):    ... 10 more
09-12 06:42:53.425: I/Process(23708): Sending signal. PID: 23708 SIG: 9

我发现错误是无法找到类StartUpReceiver。但我确保我在android清单中声明的​​类名与引用类相同。现在,我不知道如何修复错误。请帮忙。 这是代码:

public class MainActivity extends Activity implements OnClickListener {
    private ListView list;
    ArrayList<Model> data;
    ApkAdapter adapter;
    private PackageManager packageManager;
    TextView tView;
    ProtectAppProvider pap;
    CheckBox chk;
    ArrayList<String> protectedApps;
    List<PackageInfo> appsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        packageManager = getPackageManager();
        tView = (TextView) findViewById(R.id.appname);
        list = (ListView) findViewById(R.id.applist);
        appsList = getPackageManager().getInstalledPackages(0);
        data = new ArrayList<Model>();
        for (int i = 0; i < appsList.size(); i++) {
            PackageInfo p = appsList.get(i);
            if (packageManager.getLaunchIntentForPackage(p.packageName) != null
                    || (!(p.applicationInfo.loadLabel(getPackageManager())
                            .toString().equals("VoiceAppLock")))) {
                Model model = new Model();
                model.setAppName(p.applicationInfo.loadLabel(
                        getPackageManager()).toString());
                model.setAppIcon(p.applicationInfo
                        .loadIcon((getPackageManager())));
                data.add(model);

                Collections.sort(data, Model.AppComparator);

            }
        }
        adapter = new ApkAdapter(MainActivity.this, data, packageManager,
                protectedApps);
        adapter.notifyDataSetChanged();
        list.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onClick(View view) {
        boolean isChecked;
        boolean diditwork = true;
        int getPosition = (Integer) view.getTag();
        Model obj = data.get(getPosition);
        String apps = obj.getAppName();
        protectedApps = new ArrayList<String>();
        pap = new ProtectAppProvider(getApplicationContext());
        if (((CheckBox) view).isChecked()) {
            try {
                isChecked = true;
                data.get(getPosition).setSelected(isChecked);
                protectedApps.add(apps);
                pap.open();
                pap.createEntry(apps);
                pap.close();
            } catch (Exception e) {
                diditwork = false;
            } finally {
                if (diditwork)
                    Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();

            }

        } else {
            isChecked = false;
            data.get(getPosition).setSelected(isChecked);
            diditwork = true;
            try {
                pap.open();
                protectedApps.remove(apps);
                pap.deleteEntry(apps);
                pap.close();
            } catch (Exception e) {
                diditwork = false;
            } finally {
                if (diditwork)
                    Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this, "Fail to delete", Toast.LENGTH_SHORT)
                            .show();
            }
        }

    }

    private void initialize() {

        // Start receiver with the name StartupReceiver_Manual_Start
        // Check AndroidManifest.xml file
        getBaseContext().getApplicationContext().sendBroadcast(
                new Intent("StartUpReceiver_Manual_Start"));
    }
}

StartUpReceiver.java

 public class StartUpReceiver extends BroadcastReceiver {

    static final String TAG = "SR";

    final int startupID = 1111111;


    @Override
    public void onReceive(Context context, Intent intent) {

        // Create AlarmManager from System Services
        final AlarmManager alarmManager = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
        try{
                // Create pending intent for CheckRunningApplicationReceiver.class 
                // it will call after each 5 seconds

                Intent i7 = new Intent(context, LockApps.class);
                PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(context,
                        startupID, i7, 0);   
                alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                        SystemClock.elapsedRealtime(), 
                        5000, ServiceManagementIntent);


            } catch (Exception e) {
                Log.i(TAG, "Exception : "+e);
            }

        }

}

LockApps.java

public class LockApps extends BroadcastReceiver {

    public final String TAG = "CRAR";
    private ArrayList<String> protectedApps;
    private ProtectAppProvider pap;

    @Override
    public void onReceive(Context aContext, Intent anIntent) {
        protectedApps = new ArrayList<String>();
        pap = new ProtectAppProvider(aContext);
        String appname = "";
        pap.open();
        Cursor c = pap.getData();
        if (c.moveToFirst()) {
            do {
                appname = c.getString(0);
                protectedApps.add(appname);
            } while (c.moveToNext());

        }
        pap.close();

        try {

            ActivityManager am = (ActivityManager) aContext
                    .getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> li = am.getRunningTasks(1);
            Iterator<RunningTaskInfo> i = li.iterator();
            PackageManager pm = aContext.getPackageManager();
            while (i.hasNext()) {
                try {
                    ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo) (i
                            .next());
                    String ac = info.baseActivity.getPackageName();
                    CharSequence cs = pm.getApplicationLabel(pm.getApplicationInfo(ac,PackageManager.GET_META_DATA));
                    String app = cs.toString();
                    if(protectedApps.contains(app)){
                        Toast.makeText(aContext, "Hahahaa", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        catch (Throwable t) {
            Log.i(TAG, "Throwable caught: " + t.getMessage(), t);
        }
    }
}

Android Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.the.voiceapplock"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/applauncher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

        <receiver android:name=".StartUpReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="StartUpReceiver_Manual_Start" />
            </intent-filter>
        </receiver>

        <receiver android:name = ".LockApps"/>

    </application>
   <uses-permission android:name="android.permission.GET_TASKS" />


</manifest>

1 个答案:

答案 0 :(得分:0)

在主活动或用于设置全局应用变量的单例类中定义两个新方法。您可能需要使用共享首选项来获取要比较的时间值。

public void pause(Activity act) {
        // Record timeout time in case timeout service is killed    
        long time = System.currentTimeMillis();     
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putLong("Timeout_key", time);// start recording the current time as soon as app is asleep
        edit.apply();
        Timeout.start(act);

    }

    public void resume(Activity act) {
            //Timeout.cancel(act);        
        // Check whether the timeout has expired
        long cur_time = System.currentTimeMillis();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
        long timeout_start = preferences.getLong("Timeout_key", -1);
        // The timeout never started
        if (timeout_start == -1) {
            return;
        }   
        //long sTimeout = preferences.getLong("idle_delay", 30000);
        long timeout;
        try {
            //timeout = Long.parseLong(sTimeout);
            timeout=idle_delay;
        } catch (NumberFormatException e) {
            timeout = DEFAULT_TIMEOUT;
        }
        // We are set to never timeout
        if (timeout == -1) {
            return;
        }
        if (idle){
        long diff = cur_time - timeout_start;
        if (diff >= timeout) {  
            //Toast.makeText(act, "We have timed out", Toast.LENGTH_LONG).show(); 
            showLockDialog();
        }
        }
    }

}

修改基本活动中的onPause()和onResume()方法

@Override
    protected void onResume() {
        super.onResume();
        mApp.setCurrentActivity(this);
        mApp.resume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        mApp.pause(this);
    }