如何在MainActivity之前启动Java类

我正在创建一个需要在运行和启动Main Activity之前运行Java类的项目。我的Java类是:Bluetooth.java,register.java,DatabaseHelper.java。我需要运行的程序顺序:蓝牙>注册> MainActivity。MainActivity是唯一具有布局的Activity。我已经做了什么:


我删除了蓝牙中的setContentView并注册,因为它们没有布局。

我曾经打算将一项活动引向另一项活动。蓝牙注册然后注册到MainActivity

我将AndroidManifest更改为以下代码:


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.windows8.registerlogin">

<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".Bluetooth" >

    <intent-filter>

        <action android:name="android.intent.action.MAIN" />


        <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

</activity>

<activity android:name=".Register"

    android:label="@string/app_name"/>

<activity android:name=".MainActivity"

    android:label="@string/app_name"/>

</application>

</manifest>

启动程序后,它突然关闭,我无法执行任何操作。你能告诉我我的程序怎么了吗?还是我遗漏了一些东西?谢谢。(看来我无法在此处发布我的日志猫)

白衣染霜花
浏览 324回答 3
3回答

海绵宝宝撒

从应用程序类调用蓝牙类。像这样public class AppTest extends Application {@Overridepublic void onCreate() {&nbsp; &nbsp; super.onCreate();&nbsp; &nbsp;//Call your bluetooth class}}并在androidmanifest文件中添加应用程序类名称:<application&nbsp; &nbsp; android:name=".AppTest">

呼如林

使用启动器活动是个好主意,但请确保不要通过初始化内容onCreate或将上下文泄漏给匿名AsyncTask来锁定UI线程。根据您的描述,无需进行蓝牙和注册。假设这是正确的,并且您可以使它们成为独立的类,下面是一个看起来类似的示例:private AsyncTask<Void, Void, Integer> loader = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.launcher_activity);&nbsp; &nbsp; loader = new LoadStuff(this);&nbsp; &nbsp; loader.execute();}@Overridepublic void onDestroy() {&nbsp; &nbsp; super.onDestroy();&nbsp; &nbsp; if( loader != null ) {&nbsp; &nbsp; &nbsp; &nbsp; loader.cancel(true);&nbsp; &nbsp; &nbsp; &nbsp; loader = null;&nbsp; &nbsp; }}static class LoadStuff extends AsyncTask<Void, Void, Integer> {&nbsp; &nbsp; WeakReferences<Activity> context;&nbsp; &nbsp; LoadStuff(Activity context) {&nbsp; &nbsp; &nbsp; &nbsp; this.context = new WeakReference<>(context);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPreExecute() {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Integer doInBackground(Void... params) {&nbsp; &nbsp; &nbsp; &nbsp; // assuming they're singletons&nbsp; &nbsp; &nbsp; &nbsp; Bluetooth.initialize();&nbsp; &nbsp; &nbsp; &nbsp; Register.initialize();&nbsp; &nbsp; &nbsp; &nbsp; return 0; // or pass an error code if these fail&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(Integer result) {&nbsp; &nbsp; &nbsp; &nbsp; Activity c = context.get();&nbsp; &nbsp; &nbsp; &nbsp; if( c != null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(c, MainActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.finish(); // don't let the user come back to this screen&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java