【问题标题】:How to make a running service that not disturbing the activity?如何制作不干扰活动的正在运行的服务?
【发布时间】:2011-08-25 10:33:57
【问题描述】:

我有一个名为wipeScreen.java的活动

此活动运行一个wipeService.java,用于擦除Android 手机上的短信和联系人。当一个wipeService 运行时,我的wipeScreen 活动被卡住并且无法使用,只显示黑屏,甚至给我一些“强制关闭”长时间等待..

有谁知道如何创建一个可以自我工作且不干扰活动的服务,以便在服务运行时仍然可以使用活动??

这是我的擦除屏幕触发代码:

SPWipe = getSharedPreferences("wipe", 0);
editorSPWipe = SPWipe.edit();
editorSPWipe.putString("wipe", "yes");
editorSPWipe.commit();

intent myIntent = new Intent(ListenSMSservice.this,WipeScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);

这是我的wipeScreen代码:

        SPtempWipe = getSharedPreferences("wipe", 0);
        if (SPtempWipe.getString("wipe","").equalsIgnoreCase("yes"))
        {
            startService(new Intent(LockScreenForm.this, WipeService.class));
            SPWipe = getSharedPreferences("wipe", 0);
            editorSPWipe = SPWipe.edit();
            editorSPWipe.putString("wipe", "no");
            editorSPWipe.commit();
        }

这是我的wipeService代码:

@Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        wipeSMS();
        wipeContact();
        stopService(new Intent(this, WipeService.class));
    }


    public void wipeSMS()
    {
        Uri uri = Uri.parse("content://sms");
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(uri, null, null, null,
          null);

        while (cursor.moveToNext()) {
            try {
                long thread_id = cursor.getLong(1);
                Uri thread = Uri.parse("content://sms/conversations/"
                   + thread_id);
                getContentResolver().delete(thread, null, null);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    public void wipeContact() {
        // TODO Auto-generated method stub
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        while (cur.moveToNext()) {
            try{
                String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                System.out.println("The uri is " + uri.toString());
                cr.delete(uri, null, null);
            }
            catch(Exception e)
            {
                //System.out.println(e.getStackTrace());
            }
        }
    }

当服务开始删除我的联系人时,wipeScreen 活动被卡住并且无法使用..请帮助我在不干扰 wipeScreen 活动的情况下启动此服务..谢谢..

【问题讨论】:

    标签: java android android-ndk android-manifest android-service


    【解决方案1】:

    我认为您可以使用 IntentService 在单独的线程中运行您的代码

    【讨论】:

    • 感谢您的建议 Fredrik.. 我会先尝试一下.. 祝您有美好的一天.. :)\
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多