Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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
Android 我的服务在模拟器上被调用,但不是在真实设备上_Android - Fatal编程技术网

Android 我的服务在模拟器上被调用,但不是在真实设备上

Android 我的服务在模拟器上被调用,但不是在真实设备上,android,Android,当我在模拟器中运行我的应用程序并终止进程时,我的服务会启动并在后台运行(Toast:“调用服务”),但它在真实设备上根本不会被调用,也不会运行logcat,因为广播接收器或我的服务不会被调用: 维护大会: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- Service --> <service android

当我在模拟器中运行我的应用程序并终止进程时,我的服务会启动并在后台运行(Toast:“调用服务”),但它在真实设备上根本不会被调用,也不会运行logcat,因为广播接收器或我的服务不会被调用:

维护大会:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

        <!-- Service -->

        <service android:name=".MyService">
            <intent-filter>
                <action android:name="com.mypackage.myapp.MyService" />
            </intent-filter>
        </service>

        <receiver
            android:name=".BootReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!-- Service -->
PushNotification.class:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Context context = this;

            if (!isMyServiceRunning()){

                Intent serviceIntent = new Intent(context, MyService.class);
                context.startService(serviceIntent);         

                finish();

                }
            else
                {
                    finish();
                }

        }


        private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (MyService.class.getName().equals(service.service.getClassName())) {
            return true;
            }
        }
        return false;

        }
引导接收器:

            @Override
            public void onReceive(Context context, Intent intent) {
            if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent serviceIntent = new Intent(context, MyService.class);
                context.startService(serviceIntent);
                }
            }
           }
我的服务:

            public class MyService extends Service {

            Handler handler;

        @Override
        public int onStartCommand(Intent intent, int flags, int startId){
            // START YOUR TASKS
                Toast.makeText(MyService.this, "Service Called", Toast.LENGTH_SHORT).show();

                Context context = this;

                Intent in = new Intent(context, FragmentMain.class);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                in.putExtra("valuerunInBG", "1");
                context.startActivity(in);;     

                //loop();

        return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onDestroy() {
            // STOP YOUR TASKS
        super.onDestroy();
        }


        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

android.intent.action.BOOT_完成需要时间设备启动后,请尝试在服务类的
onDestroy()方法中等待3-4分钟

再次启动服务。这样,当您的应用程序关闭时,将调用
onDestroy()
,这将再次启动您的服务。

修改您的清单,如下所示

<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter android:priority="1000">
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
 </intent-filter>
</receiver>

有些时候,Toast在服务内部不起作用,因此请使用日志来了解您的代码是否正在运行。

在我重新启动手机后会调用服务,但在我关闭应用程序时不会调用该服务要在应用程序关闭时调用服务,请查看我的答案,继续本教程我也需要同样的东西,本教程帮助我确定了此答案有点帮助,不像在我的模拟器上,在onDestroy上再次调用我的服务,每次我关闭我的应用程序时都会将其调用到UI,而不是在后台运行服务。你说的UI everytime是什么意思?在emulator上,我只能在应用程序未启动时收到toast消息。如果我模拟数据库中的消息,我仍然可以收到通知,但在真实设备上,应用程序会加载并启动时,就像用户在销毁方法非活动的服务类中单击iconJust start service一样。我更喜欢这种修改,服务也会在UI中启动应用程序。与我的模拟器不同,在模拟器中,服务在后台启动应用程序,对服务在后台启动应用程序有何建议(同样,不是最小化,而是在后台,应用程序不会显示为正在运行)在android中,您有两个类来运行代码:一个是提供ui的活动,另一个是在后台运行的服务,可以根据服务的onstartcommand方法中的代码在特定条件下启动活动。如果它在清单中声明为activitylearn,则将启动FragmentMain.class访问此链接了解有关服务的更多信息n实际上,我使用loop()进行测试,它只在emulator上工作
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter android:priority="1000">
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
 </intent-filter>
</receiver>
@Override
    public int onStartCommand(Intent intent, int flags, int startId){
        // START YOUR TASKS
            Toast.makeText(MyService.this, "Service Called", Toast.LENGTH_SHORT).show();

            Context context = this;

            Intent in = new Intent(context, FragmentMain.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            in.putExtra("valuerunInBG", "1");
            context.startActivity(in);;     

            //loop();

    return  START_STICKY; //THIS WILL RESTART YOUR SERVICE IF IT GETS STOPPED BY ANDROID OS DUE TO LOW MEMORY
    }