纯控制台Android应用程序? [英] Pure console Android Application?
问题描述
是否可以创建将在android模拟器中运行的纯控制台android应用程序?
Is it possible to create a pure console android application that will run in the android emulator?
我的意思是我们可以运行使用 System.out.println
进行控制台输出的经典桌面Java应用程序,所以我不明白为什么我们不能通过 android.util.Log
类.
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.
在仿真器上执行此操作的优点是可以访问由Android Java类实现的所需功能.
The advantages of doing it on an emulator will be that gives access to the desired functionality implemented by Android Java classes.
也许是一个没有 Application
, Activity
类和 AndroidManifest.xml
Perhaps a dex file without the Application
, Activity
class and AndroidManifest.xml
如何最好地做到这一点?
How to best do this?
推荐答案
没有"AndroidMain"方法.您可以使用主要的Activity来完成此操作,而无需使用UI或启动服务.
There is no "AndroidMain" method. You can accomplish this using a main Activity without UI or launching a Service.
例如
AndroidManifest.xml
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
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
MyService.java
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("MyService", "Hello world!");
}
}
这篇关于纯控制台Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!