不幸的是,应用程序在线程中使用服务时已停止
unfortunately app has stopped while using service in thread
我是 android 开发的新手,正在尝试在服务上制作演示应用程序。
但是正如教程所描述的那样,服务在主 UI 线程上 运行,我创建了线程并将我的服务放在该线程中以在后台执行。它在后台也能正常工作几秒钟,然后通过说 "unfortunately app has stopped".
关闭应用程序
这是我的代码,
服务class
public class UpdateLocation extends Service {
private class UpdateLocationThread implements Runnable{
int service_id;
UpdateLocationThread(int service_id){
this.service_id = service_id;
}
@Override
public void run() {
int i= 0;
synchronized (this){
while (i <= 10){
try {
wait(15000);
i++;
Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
stopSelf(service_id);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(UpdateLocation.this, "Service started...", Toast.LENGTH_SHORT).show();
Thread thread = new Thread(new UpdateLocationThread(startId));
thread.start();
return START_STICKY;
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.keval.mejodo" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".Model.commonFuncs"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <!-- android:theme="@style/Theme.AppCompat.NoActionBar" -->
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyAppBaseActivity"
android:label="@string/title_activity_my_app_base" >
</activity>
<service android:name=".MServices.UpdateLocation"
android:exported="false"></service>
</application>
</manifest>
边做边调用服务,
Intent intent = new Intent(this, UpdateLocation.class);
startService(intent);
停止申请的原因可能是什么?如何解决?
您不能在 UI 线程上显示 Toast,如果有必要,请按
runOnUiThread(new Runnable() {
@Override
public void run() {
//yourtoast;
}
});
您正在线程内显示 Toast,这仅在 UI 线程中允许,将其删除或在 UI 主线程中调用它。
我是 android 开发的新手,正在尝试在服务上制作演示应用程序。 但是正如教程所描述的那样,服务在主 UI 线程上 运行,我创建了线程并将我的服务放在该线程中以在后台执行。它在后台也能正常工作几秒钟,然后通过说 "unfortunately app has stopped".
关闭应用程序这是我的代码,
服务class
public class UpdateLocation extends Service {
private class UpdateLocationThread implements Runnable{
int service_id;
UpdateLocationThread(int service_id){
this.service_id = service_id;
}
@Override
public void run() {
int i= 0;
synchronized (this){
while (i <= 10){
try {
wait(15000);
i++;
Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
stopSelf(service_id);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(UpdateLocation.this, "Service started...", Toast.LENGTH_SHORT).show();
Thread thread = new Thread(new UpdateLocationThread(startId));
thread.start();
return START_STICKY;
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.keval.mejodo" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".Model.commonFuncs"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <!-- android:theme="@style/Theme.AppCompat.NoActionBar" -->
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyAppBaseActivity"
android:label="@string/title_activity_my_app_base" >
</activity>
<service android:name=".MServices.UpdateLocation"
android:exported="false"></service>
</application>
</manifest>
边做边调用服务,
Intent intent = new Intent(this, UpdateLocation.class);
startService(intent);
停止申请的原因可能是什么?如何解决?
您不能在 UI 线程上显示 Toast,如果有必要,请按
runOnUiThread(new Runnable() {
@Override
public void run() {
//yourtoast;
}
});
您正在线程内显示 Toast,这仅在 UI 线程中允许,将其删除或在 UI 主线程中调用它。