Гость
Восстановите или поддержите соединение между Android и базой данных MySQL.
Сообщение
Гость » 09 мар 2024, 20:45
I am creating an app with a html, css, js inside a webview, and also several databases to store data. My app is designed to be used for 10 to 11 hours each time.
A big problem was found that android will kill my app in background after leaving for a while. However, app gets killed will lead to a big problem as it means the whole task has to redo again. I have to maintain OR rebuild the connection between database and android to ensure my app can keep running smoothly.
I would like to know:
1.) is there any way to maintain the connection between database and android (e.g. put it in foreground service?) so that killing app will not affect the connection?
2.) can I ask android and js to rebuild the connection (e.g. synchronize the database?) between android and js after the app get killed by OS?
Here is my MainActivity:
Код: Выделить всё
public class MainActivity extends AppCompatActivity
{
HTMLDataBase myDB1;
ContentTableDataBase myDB2;
CloudTableDataBase myDB3;
InterviewDataBase myDB4;
Context context;
boolean check;
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB1 = new HTMLDataBase(this);
myDB2 = new ContentTableDataBase(this);
myDB3 = new CloudTableDataBase(this);
myDB4 = new InterviewDataBase(this);
display_html();
Javascript_Android_Communication();
}
@Override
protected void onResume()
{
super.onResume();
myDB1 = new HTMLDataBase(this);
myDB2 = new ContentTableDataBase(this);
myDB3 = new CloudTableDataBase(this);
myDB4 = new InterviewDataBase(this);
display_html();
Javascript_Android_Communication();
}
@Override
protected void onStop()
{
super.onStop();
this.finish();
}
public void display_html()
{
WebView view = (WebView) findViewById(R.id.WebView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl("file:///android_asset/index.html");
}
public void Javascript_Android_Communication()
{
WebView view = (WebView) findViewById(R.id.WebView);
view.setWebChromeClient(new WebChromeClient() {});
WebSettings settings = view.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
view.addJavascriptInterface(new JavaScriptInterface(this), "Android");
view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
}
}
WebViewJavaScriptInterface:
Код: Выделить всё
public class WebViewJavaScriptInterface
{
HTMLDataBase myDB1;
ContentTableDataBase myDB2;
CloudTableDataBase myDB3;
InterviewDataBase myDB4;
Context mContext;
WebViewJavaScriptInterface (Context c)
{
mContext = c;
}
@JavascriptInterface
public void Javascript_to_Android(String data_id, String html_element, String state)
{
myDB1 = new HTMLDataBase((MainActivity) mContext);
myDB1.updateData(data_id, html_element, state);
}
@JavascriptInterface
public void Javascript_to_Content_Table_DataBase(String data_id, String duty_time, String my_duty, String checkbox)
{
myDB2 = new ContentTableDataBase((MainActivity) mContext);
myDB2.insertData(data_id, duty_time, my_duty, checkbox);
}
@JavascriptInterface
public void add_cloud_data(String data_id, String Time, String Total_Cloud, String Low_Medium_Cloud, String FirstCloud,
String SecondCloud, String ThirdCloud, String ForthCloud, String FifthCloud, String SixthCloud,
String SeventhCloud, String EighthCloud, String Visibility)
{
myDB3 = new CloudTableDataBase((MainActivity) mContext);
boolean response = myDB3.insertData(data_id, Time, Total_Cloud, Low_Medium_Cloud, FirstCloud, SecondCloud, ThirdCloud, ForthCloud, FifthCloud, SixthCloud, SeventhCloud, EighthCloud, Visibility);
if (response == true)
{
Toast.makeText(mContext, "Data inserted", Toast.LENGTH_SHORT).show();
}
if (response == false)
{
Toast.makeText(mContext, "Data not inserted", Toast.LENGTH_SHORT).show();
}
}
@JavascriptInterface
public void update_cloud_data(String data_id, String Time, String Total_Cloud, String Low_Medium_Cloud, String FirstCloud,
String SecondCloud, String ThirdCloud, String ForthCloud, String FifthCloud, String SixthCloud,
String SeventhCloud, String EighthCloud, String Visibility)
{
myDB3 = new CloudTableDataBase((MainActivity) mContext);
boolean response = myDB3.updateData(data_id, Time, Total_Cloud, Low_Medium_Cloud, FirstCloud, SecondCloud, ThirdCloud, ForthCloud, FifthCloud, SixthCloud, SeventhCloud, EighthCloud, Visibility);
if (response == true)
{
Toast.makeText(mContext, "Data updated", Toast.LENGTH_SHORT).show();
}
if (response == false)
{
Toast.makeText(mContext, "Data not updated", Toast.LENGTH_SHORT).show();
}
}
@JavascriptInterface
public void delete_duty(String id)
{
myDB2.deleteData(id);
}
@JavascriptInterface
public void delete_cloud(String id)
{
myDB3.deleteData(id);
}
}
Источник:
https://stackoverflow.com/questions/781 ... l-database
1710006303
Гость
I am creating an app with a html, css, js inside a webview, and also several databases to store data. My app is designed to be used for 10 to 11 hours each time. A big problem was found that android will kill my app in background after leaving for a while. However, app gets killed will lead to a big problem as it means the whole task has to redo again. I have to maintain OR rebuild the connection between database and android to ensure my app can keep running smoothly. I would like to know: 1.) is there any way to maintain the connection between database and android (e.g. put it in foreground service?) so that killing app will not affect the connection? 2.) can I ask android and js to rebuild the connection (e.g. synchronize the database?) between android and js after the app get killed by OS? Here is my MainActivity: [code]public class MainActivity extends AppCompatActivity { HTMLDataBase myDB1; ContentTableDataBase myDB2; CloudTableDataBase myDB3; InterviewDataBase myDB4; Context context; boolean check; private WebView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDB1 = new HTMLDataBase(this); myDB2 = new ContentTableDataBase(this); myDB3 = new CloudTableDataBase(this); myDB4 = new InterviewDataBase(this); display_html(); Javascript_Android_Communication(); } @Override protected void onResume() { super.onResume(); myDB1 = new HTMLDataBase(this); myDB2 = new ContentTableDataBase(this); myDB3 = new CloudTableDataBase(this); myDB4 = new InterviewDataBase(this); display_html(); Javascript_Android_Communication(); } @Override protected void onStop() { super.onStop(); this.finish(); } public void display_html() { WebView view = (WebView) findViewById(R.id.WebView); view.getSettings().setJavaScriptEnabled(true); view.getSettings().setDomStorageEnabled(true); view.loadUrl("file:///android_asset/index.html"); } public void Javascript_Android_Communication() { WebView view = (WebView) findViewById(R.id.WebView); view.setWebChromeClient(new WebChromeClient() {}); WebSettings settings = view.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); view.addJavascriptInterface(new JavaScriptInterface(this), "Android"); view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } } [/code] WebViewJavaScriptInterface: [code] public class WebViewJavaScriptInterface { HTMLDataBase myDB1; ContentTableDataBase myDB2; CloudTableDataBase myDB3; InterviewDataBase myDB4; Context mContext; WebViewJavaScriptInterface (Context c) { mContext = c; } @JavascriptInterface public void Javascript_to_Android(String data_id, String html_element, String state) { myDB1 = new HTMLDataBase((MainActivity) mContext); myDB1.updateData(data_id, html_element, state); } @JavascriptInterface public void Javascript_to_Content_Table_DataBase(String data_id, String duty_time, String my_duty, String checkbox) { myDB2 = new ContentTableDataBase((MainActivity) mContext); myDB2.insertData(data_id, duty_time, my_duty, checkbox); } @JavascriptInterface public void add_cloud_data(String data_id, String Time, String Total_Cloud, String Low_Medium_Cloud, String FirstCloud, String SecondCloud, String ThirdCloud, String ForthCloud, String FifthCloud, String SixthCloud, String SeventhCloud, String EighthCloud, String Visibility) { myDB3 = new CloudTableDataBase((MainActivity) mContext); boolean response = myDB3.insertData(data_id, Time, Total_Cloud, Low_Medium_Cloud, FirstCloud, SecondCloud, ThirdCloud, ForthCloud, FifthCloud, SixthCloud, SeventhCloud, EighthCloud, Visibility); if (response == true) { Toast.makeText(mContext, "Data inserted", Toast.LENGTH_SHORT).show(); } if (response == false) { Toast.makeText(mContext, "Data not inserted", Toast.LENGTH_SHORT).show(); } } @JavascriptInterface public void update_cloud_data(String data_id, String Time, String Total_Cloud, String Low_Medium_Cloud, String FirstCloud, String SecondCloud, String ThirdCloud, String ForthCloud, String FifthCloud, String SixthCloud, String SeventhCloud, String EighthCloud, String Visibility) { myDB3 = new CloudTableDataBase((MainActivity) mContext); boolean response = myDB3.updateData(data_id, Time, Total_Cloud, Low_Medium_Cloud, FirstCloud, SecondCloud, ThirdCloud, ForthCloud, FifthCloud, SixthCloud, SeventhCloud, EighthCloud, Visibility); if (response == true) { Toast.makeText(mContext, "Data updated", Toast.LENGTH_SHORT).show(); } if (response == false) { Toast.makeText(mContext, "Data not updated", Toast.LENGTH_SHORT).show(); } } @JavascriptInterface public void delete_duty(String id) { myDB2.deleteData(id); } @JavascriptInterface public void delete_cloud(String id) { myDB3.deleteData(id); } } [/code] Источник: [url]https://stackoverflow.com/questions/78127476/rebuild-or-maintain-a-connection-between-android-and-mysql-database[/url]