-1

I try to create a service without ui and i want to start that but when i delete activity tags on manifest i cannot start my application it returns to me Could not identify launch activity: Default Activity not found error.

What can i do?

Here is my Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.test.myservice">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:label="@string/app_name">

    <service
        android:name=".myservice.SocketService"
        android:exported="true"
        android:enabled="true">
    </service>
    <receiver android:enabled="true" android:name=".myservice.StartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </receiver>

</application>

Here is my Receiver class

class StartReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
    if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
        Intent(context, MyService::class.java).also {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startService(it)
                return
            }
            context.startService(it)
        }
    }
}

}
saulyasar
  • 797
  • 1
  • 17
  • 45
  • I think [this](https://stackoverflow.com/questions/4467600/how-to-launch-a-android-service-when-the-app-launches) might be helpful. – Kalai Dec 02 '20 at 13:56
  • 1
    When initially installed, your app is in a "stopped" state, akin to as if the user pressed Force Stop on your app's screen in Settings. Nothing in your app will run until something uses an *explicit* `Intent` to start one of your app's components. In most apps, that will be the launcher starting one of your activities. In your case, you lack that activity. Your `` is primarily used for an *implicit* `Intent`. So, most likely, your app will never run after being installed. – CommonsWare Dec 02 '20 at 14:00
  • @CommonsWare what you suggest? – saulyasar Dec 02 '20 at 14:11
  • I suggest that you add a standard launcher activity. You need one for the privacy policy, and for directing people to technical support, and for configuring whatever it is that your service is doing. Also, note that `android.permission.WRITE_INTERNAL_STORAGE` and `android.permission.READ_INTERNAL_STORAGE` do not exist, so you can remove those `` elements. – CommonsWare Dec 02 '20 at 14:23

2 Answers2

0

How are you launching your application? From the manifest file, looks like you are listening to BOOT_COMPLETED intent and then probably launching the Service from the BroadCastReceiver. So if you reboot the device, your app will be launched via the BroadCastReceiver on boot complete event. However, since there is no Activity in your application, it can't be started via Launcher app tray or similar means which launches Activity. Check the logcat logs during device boot up to see if your app is getting launched.

To launch the service directly via adb shell try below

adb shell am startservice -n com.test.myservice/.myservice.SocketService

See https://developer.android.com/studio/command-line/adb#am

coderms
  • 96
  • 6
  • Actually ,I'm working on emulator and i'm just try to execute m app from Android studio and getting that error – saulyasar Dec 02 '20 at 13:59
  • ok. I see some answers already on changing Android studio configurations. You could try that. There is one method to directly launch activity/service via adb. After you install your app from studio (by launching it from studio) try below command. "adb shell am startservice -n com.test.myservice/.myservice.SocketService". See https://developer.android.com/studio/command-line/adb#am – coderms Dec 02 '20 at 14:12
  • Edited the comment. accidently pressed enter – coderms Dec 02 '20 at 14:14
  • returns this 'adb' is not recognized as an internal or external command, – saulyasar Dec 02 '20 at 14:30
  • adb executable is included in the Android SDK Platform-Tools package. You can download this package with the SDK Manager, which installs it at android_sdk/platform-tools/. https://developer.android.com/studio/command-line/adb – coderms Dec 02 '20 at 14:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225417/discussion-between-coderms-and-saulyasar). – coderms Dec 02 '20 at 15:09
0

set lunch option to Noting in Run configuration setting.

look at this link : https://www.programmersought.com/article/5560819605/

saeedata
  • 901
  • 6
  • 14