我尝试创建我的第一个服务,当然没有成功。但这个应用程序成功编译,并没有在AndroidMonitor中给出任何错误。 简而言之,按钮没有反应按压。 我添加到Android Manifest所需的行:
+----+-----------+-------+
| id | timestamp | count |
+----+-----------+-------+
| 1 | 1970 | 3 |
| 2 | 1971 | 2 |
+----+-----------+-------+
ClientActivity
<service android:name=".MyService"></service>
为MyService
package com.example.servicetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ClientActivity extends Activity {
private Button btnStartService;
private Button btnStopService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
initButtonsOnClick();
}
private void initButtonsOnClick() {
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartService:
startMyService();
break;
case R.id.btnStopService:
stopMyService();
break;
default:
break;
}
}
};
btnStartService.setOnClickListener(listener);
btnStopService.setOnClickListener(listener);
}
private void startMyService() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
private void stopMyService() {
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
}
}
答案 0 :(得分:2)
服务是一个可以在后台执行长时间运行的应用程序组件,而它不提供用户界面。 - 来自官方Android文档。
现在,由于服务没有ui,因此在使用服务时无法显示吐司。
答案 1 :(得分:1)
Toast 始终与Application的主线程一起使用。所以你必须确保在主线程中使用线程。
如果你想在服务中使用Toast,那么你必须使用Handler。
请查看以下关于Handler in Thread的示例:
private Context mContext;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mContext=getApplicationContext();//Get the context here
}
//Use this method to show toast
void showToast(){
if(mContext != null){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run()
{
Toast.makeText(mContext, "Display your message here", Toast.LENGTH_SHORT)show();
}
});
}
}
答案 2 :(得分:0)
public class ClientActivity extends Activity {
private Button btnStartService;
private Button btnStopService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
});
btnStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
}
});
}
}
使用此按钮单击
DisplayOrientations currentOrientation =
Windows.Graphics.Display.DisplayProperties.CurrentOrientation;
答案 3 :(得分:0)
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"helllo",
Toast.LENGTH_SHORT).show();
}
});
答案 4 :(得分:0)
在您的服务中添加以下代码
if (getActivity()!=null){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "Your Text", Toast.LENGTH_SHORT).show();
}
});
}