Android服务未从活动开始
单击按钮后,我正试图从Android服务未从活动开始,android,android-service,Android,Android Service,单击按钮后,我正试图从活动启动服务。但是服务没有启动。我的想法是使用处理程序将服务中的一些数据发布到活动 我的AndroidManifest.xml文件: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
活动
启动服务
。但是服务没有启动。我的想法是使用处理程序将服务
中的一些数据发布到活动
我的AndroidManifest.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myfirstandroidapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true">
<activity android:name=".servicesdemo.CountrySearchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.myfirstandroidapp.servicesdemo.MyRESTRequestService"
android:enabled="false"
android:exported="false" ></service>
</application>
</manifest>
我的活动
课程:
public class CountrySearchActivity extends AppCompatActivity implements MyHandler.IUIUpdater {
@BindView(R.id.btn_get_country_from_webservice)
Button btnGetCountryFromWebService;
@BindView(R.id.tv_container_country_name)
EditText countryNameEntryField;
@BindView(R.id.tv_obtained_country)
TextView resultingCountry;
private void getCountryFromWebService() {
String countryNameForGetting = countryNameEntryField
.getText()
.toString();
Handler handler = new MyHandler(this);
Intent intent = new Intent(getApplicationContext(),
MyRESTRequestService.class);
intent.putExtra("COUNTRY_NAME", countryNameForGetting);
intent.putExtra("HANDLER", new Messenger(handler));
ContextCompat.startForegroundService(getApplicationContext(),
intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country_search);
ButterKnife.bind(this);
btnGetCountryFromWebService.setOnClickListener(v ->
getCountryFromWebService());
}
@Override
public void updateTheUi(Message message) {
resultingCountry.setText((String) message.obj);
stopService(new Intent(getApplicationContext(),
MyRESTRequestService.class));
}
}
public class MyHandler extends Handler {
IUIUpdater iuiUpdater;
public MyHandler(IUIUpdater iuiUpdater) {
this.iuiUpdater = iuiUpdater;
}
@Override
public void handleMessage(Message msg) {
iuiUpdater.updateTheUi(msg);
}
public interface IUIUpdater {
void updateTheUi(Message message);
}
}
我的处理程序
类:
public class CountrySearchActivity extends AppCompatActivity implements MyHandler.IUIUpdater {
@BindView(R.id.btn_get_country_from_webservice)
Button btnGetCountryFromWebService;
@BindView(R.id.tv_container_country_name)
EditText countryNameEntryField;
@BindView(R.id.tv_obtained_country)
TextView resultingCountry;
private void getCountryFromWebService() {
String countryNameForGetting = countryNameEntryField
.getText()
.toString();
Handler handler = new MyHandler(this);
Intent intent = new Intent(getApplicationContext(),
MyRESTRequestService.class);
intent.putExtra("COUNTRY_NAME", countryNameForGetting);
intent.putExtra("HANDLER", new Messenger(handler));
ContextCompat.startForegroundService(getApplicationContext(),
intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country_search);
ButterKnife.bind(this);
btnGetCountryFromWebService.setOnClickListener(v ->
getCountryFromWebService());
}
@Override
public void updateTheUi(Message message) {
resultingCountry.setText((String) message.obj);
stopService(new Intent(getApplicationContext(),
MyRESTRequestService.class));
}
}
public class MyHandler extends Handler {
IUIUpdater iuiUpdater;
public MyHandler(IUIUpdater iuiUpdater) {
this.iuiUpdater = iuiUpdater;
}
@Override
public void handleMessage(Message msg) {
iuiUpdater.updateTheUi(msg);
}
public interface IUIUpdater {
void updateTheUi(Message message);
}
}
我进行了调试,在调用ContextCompat.startForegroundService(getApplicationContext(),intent)
之后,没有看到服务类中出现任何断点。断点被放置在服务
的构造函数、onCreate()
方法(我在这里没有显示,但它在那里)、onStart命令()
方法上。您必须显示通知并请求前台服务
权限才能启动前台服务。
请检查您必须显示通知并请求前台服务
权限才能启动前台服务。
请检查我在您的服务课上所做的更改。请试试这个
在CountrySearchActivity类中:
String countryNameForGetting = countryNameEntryField
.getText()
.toString();
Handler handler = new MyHandler(this);
Intent intent = new Intent(getApplicationContext(),
MyRESTRequestService.class);
intent.putExtra("COUNTRY_NAME", countryNameForGetting);
intent.putExtra("HANDLER", new Messenger(handler));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ContextCompat.startForegroundService(this, intent);
else
startService(intent);
服务类别:
在AndroidManifest.xml中
我在你们的服务班换了。请试试这个
在CountrySearchActivity类中:
String countryNameForGetting = countryNameEntryField
.getText()
.toString();
Handler handler = new MyHandler(this);
Intent intent = new Intent(getApplicationContext(),
MyRESTRequestService.class);
intent.putExtra("COUNTRY_NAME", countryNameForGetting);
intent.putExtra("HANDLER", new Messenger(handler));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ContextCompat.startForegroundService(this, intent);
else
startService(intent);
服务类别:
在AndroidManifest.xml中
请尝试以下方式:startForegroundService(getApplicationContext(),intent)代码>替换为开始服务(意图)
@nikunsorathiyastartForegroundService()
内部调用startService()
,具体取决于Android版本。此外,我最初尝试了startService()
。如果(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){startForegroundService(getApplicationContext(),intent);}或者{startService(intent);}@nikunsorathiya是的,这就是它在内部的工作方式。我的目标平台是牛轧糖。所以当我调用startForegroundService()
时,实际上我使用的是startService()
您是否在AndroidManifest文件中添加了前台服务
权限?如果没有,请添加权限
请尝试以下方式:启动ForegroundsService(getApplicationContext(),intent)代码>替换为开始服务(意图)
@nikunsorathiyastartForegroundService()
内部调用startService()
,具体取决于Android版本。此外,我最初尝试了startService()
。如果(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){startForegroundService(getApplicationContext(),intent);}或者{startService(intent);}@nikunsorathiya是的,这就是它在内部的工作方式。我的目标平台是牛轧糖。所以当我调用startForegroundService()
时,实际上我使用的是startService()
您是否在AndroidManifest文件中添加了前台服务
权限?如果没有,那么请添加权限
我指的是本博客最后一种解决方案:另外,我添加了前台服务
权限。还是一样的结果。我指的是这个博客的最后一种解决方案:另外,我添加了前台服务
权限。还是一样的结果。