3 回答
TA贡献1809条经验 获得超8个赞
从应用程序类调用蓝牙类。像这样
public class AppTest extends Application {
@Override
public void onCreate() {
super.onCreate();
//Call your bluetooth class
}
}
并在androidmanifest文件中添加应用程序类名称:
<application
android:name=".AppTest">
TA贡献1798条经验 获得超3个赞
使用启动器活动是个好主意,但请确保不要通过初始化内容onCreate或将上下文泄漏给匿名AsyncTask来锁定UI线程。根据您的描述,无需进行蓝牙和注册。假设这是正确的,并且您可以使它们成为独立的类,下面是一个看起来类似的示例:
private AsyncTask<Void, Void, Integer> loader = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher_activity);
loader = new LoadStuff(this);
loader.execute();
}
@Override
public void onDestroy() {
super.onDestroy();
if( loader != null ) {
loader.cancel(true);
loader = null;
}
}
static class LoadStuff extends AsyncTask<Void, Void, Integer> {
WeakReferences<Activity> context;
LoadStuff(Activity context) {
this.context = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(Void... params) {
// assuming they're singletons
Bluetooth.initialize();
Register.initialize();
return 0; // or pass an error code if these fail
}
@Override
protected void onPostExecute(Integer result) {
Activity c = context.get();
if( c != null ) {
Intent intent = new Intent(c, MainActivity.class);
c.startActivity(intent);
c.finish(); // don't let the user come back to this screen
}
}
}
添加回答
举报