Pure console Android Application?

拜拜、爱过 提交于 2021-01-27 21:08:02

问题


Is it possible to create a pure console android application that will run in the android emulator?

I mean we can run classic desktop Java application that utilize System.out.println for console output, so I don't see why we are not able to do the same for Android via the android.util.Log classes.

The advantages of doing it on an emulator will be that gives access to the desired functionality implemented by Android Java classes.

Perhaps a dex file without the Application, Activity class and AndroidManifest.xml

How to best do this?


回答1:


There is no "AndroidMain" method. You can accomplish this using a main Activity without UI or launching a Service.

E.g.

AndroidManifest.xml

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MyService" />
</application>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(this, MyService.class);
        startService(i);
        finish();
    }
}

MyService.java

public class MyService extends IntentService {

    public MyService() {
        super("MyService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("MyService", "Hello world!");
    }
}


来源:https://stackoverflow.com/questions/39819703/pure-console-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!