如何制作一个不干扰Activity的正在运行的服务?

发布于 2024-12-01 14:50:31 字数 2746 浏览 6 评论 0原文

我有一个名为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);

这是我的擦拭屏幕代码:

        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();
        }

这是我的擦拭服务代码:

@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());
            }
        }
    }

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

i have an activity called wipeScreen.java

this activity runs a wipeService.java for wiping a SMS and a contacts on android mobile phone. when a wipeService running, my wipeScreen activity is stuck and can't be used, only showing a black screen, and even give me some "force close" for long waiting time..

has anyone know how to create a service that can be self-working and not disturbing the activity so the activity still can be used when the service is running??

here's my wipescreen trigger code:

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);

here's my wipeScreen code:

        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();
        }

and here's my wipeService code :

@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());
            }
        }
    }

when a service started to removing my contact, the wipeScreen activity is stuck and can't be used.. please help me to make this service started without disturbing the wipeScreen activity.. thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-12-08 14:50:31

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

I think you could use an IntentService to run your code in a separate thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文