简体   繁体   English

Android服务中的启动意图活动错误

[英]Android starting intent activity error in service

In my application, there is a service class. 在我的应用程序中,有一个服务类。 I want to start an intent 10 seconds later with this service. 我想在10秒钟后使用此服务开始意图。 When I try a basic toast message instead of starting intent, it works. 当我尝试基本的祝酒消息而不是开始意图时,它会起作用。 However, when I write startActivity(intent) an error occurs. 但是,当我编写startActivity(intent)时,会发生错误。 Here is my code. 这是我的代码。 Where am I making mistake? 我在哪里犯错?

public class EkraniKilitle extends Service {

    Handler yardimci;
    Intent kilitEkrani;
    final static long ZAMAN = 10000;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();

        kilitEkrani = new Intent(EkraniKilitle.this, KilitEkrani.class);

        yardimci = new Handler();
        yardimci.postDelayed(new Runnable() {
            @Override
            public void run() {

                servisiDurdur();
                startActivity(kilitEkrani);

            }
        }, ZAMAN);

    }

    public void servisiDurdur() {

        stopService(new Intent(this, EkraniKilitle.class));

    }

    @Override
    public void onDestroy() {

        super.onDestroy();

    }

}

i guess you need to add your second activity to the manifest 我想您需要将第二项活动添加到manifest
after </activity> add <activity android:name=".MySecondClass"/> </activity>添加<activity android:name=".MySecondClass"/>

Call startActivity(kilitEkrani); 呼叫startActivity(kilitEkrani); before servisiDurdur(); servisiDurdur(); 之前 servisiDurdur(); and to stop your current service all what you need is to call stopSelf() you do not have to use stopService 并停止当前服务,您只需调用stopSelf() ,而不必使用stopService

Thank you for all your answers. 感谢您的所有答复。 I've solved my problem via this link: android start activity from service 我已经通过以下链接解决了我的问题: 服务中的android启动活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM