Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中如何运行后台服务?_Java_Android_Android Intent - Fatal编程技术网

Java 在Android中如何运行后台服务?

Java 在Android中如何运行后台服务?,java,android,android-intent,Java,Android,Android Intent,你能告诉我在运行后台服务的代码中犯了什么错误吗?还有其他代码可以运行后台服务吗。 以下程序正确吗。。。。如何在android中运行后台服务 FrontEnd.java: ***************** import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickLi

你能告诉我在运行后台服务的代码中犯了什么错误吗?还有其他代码可以运行后台服务吗。 以下程序正确吗。。。。如何在android中运行后台服务

FrontEnd.java:
*****************

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 FrontEnd extends Activity implements OnClickListener {

    private Button btnStartService, btnStopService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frontend);

        bindComponents();
        addListener();
        init();
    }

    private void bindComponents() {
        btnStartService = (Button)findViewById(R.id.btnStartService);
        btnStopService = (Button)findViewById(R.id.btnStopService);
    }

    private void addListener() {
        btnStartService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
    }

    private void init() {

    }

    public void onClick(View view) {
        Intent mIntent = new Intent(this,BackEndService.class);
        System.out.println("------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<");
        switch (view.getId()) {
        case R.id.btnStartService:
            startService(mIntent);
            break;
        case R.id.btnStopService:
            stopService(mIntent);
            break;
        default:
            break;
        }
    }
}

******************************************************************************************************************

BackEndService.java
*********************

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BackEndService extends Service{

    private static final String tag = "Panacea";

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(tag, "onBind");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(tag, "onCreate");

        Notification notification = new Notification(R.drawable.launcher, getText(R.string.app_name),
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, GmaTestApp01Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(this, "BackGround Service", "Project is running", pendingIntent);
        startForeground(1, notification);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(tag, "onStart startId=" + startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(tag, "onStartCommand startId=" + startId);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(tag, "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(tag, "onDestroy");
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }
}

*******************************************************************************************************************
frontend.xml
***************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
   >



    <Button
        android:id="@+id/btnStartService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start_service"
         />



    <Button
        android:id="@+id/btnStopService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop_service" />

</LinearLayout>
FrontEnd.java:
*****************
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
公共类前端扩展活动实现OnClickListener{
专用按钮btnStartService、btnStopService;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局前端);
绑定组件();
addListener();
init();
}
私有组件(){
btnStartService=(按钮)findViewById(R.id.btnStartService);
btnStopService=(按钮)findViewById(R.id.btnStopService);
}
私有void addListener(){
btnStartService.setOnClickListener(此);
setOnClickListener(这个);
}
私有void init(){
}
公共void onClick(视图){
Intent MINENT=新的Intent(这个,BackEndService.class);

System.out.println(“----------------------------------->>>>>>>>>>>>>>>>>如果BackEndService服务未运行,可能是因为您忘记在清单文件中提及它。如果是,请添加以下内容

    <application 
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".YourApp" >

   <activity ....>
    ...
    </activity>

    <service android:name=".BackEndService"></service>
    ....
    </application>

...
....

您的代码似乎是正确的。

自Android 8以来,在没有通知的情况下运行后台服务将无法工作。

您仔细阅读了文档吗?您所犯的错误是什么意思?您没有得到想要的吗?您是否收到任何错误在上面编写的代码中?@Ree,我建议您先阅读文档。我在清单文件中包含了此标记。但上面的代码仅为前台服务运行,而不是后台服务否,是您的GmaTestApp01Activity活动在前台运行。您的服务在后台运行,并向您的GmaTest发送通知App01Activity.GmaTestApp01Activity是用于在血糖仪中读取读数的Java文件。我的要求是在后台运行血糖仪ie GmaTestApp01Activity文件,在为患者读取血糖仪读数时,必须在前台单独显示读数。请为主页代码更新编码
    <application 
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".YourApp" >

   <activity ....>
    ...
    </activity>

    <service android:name=".BackEndService"></service>
    ....
    </application>