我的服务在模拟器上调用但不是真正的设备

时间:2016-08-22 07:17:37

标签: android

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

MainFest:

    <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 -->

MainActivity:

               AsyncTask.execute(new Runnable() {
               @Override
               public void run() {
                  //TODO your background code
                    Intent i = new Intent(MainActivity.this, PushNotification.class);
                    startActivity(i);
               }
            });

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;

        }

BootReceiver:

            @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);
                }
            }
           }

为MyService:

            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;
        }

5 个答案:

答案 0 :(得分:1)

修改您的清单,如下所示

<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
    }

答案 1 :(得分:0)

android.intent.action.BOOT_COMPLETED设备启动后需要一段时间,请等待3-4分钟

答案 2 :(得分:0)

尝试添加;

public enum TestEnum { TESTONE,TESTTWO, NONE; public String toString() { switch(this) { case TESTONE: return "Test one"; case TESTTWO: return "Test two"; case NONE: return "None"; } return null; }; public static TestEnum valueOf(Class<TestEnum> enumType, String value){ if(value.equalsIgnoreCase(TESTONE.toString())) return TestEnum.TESTONE; else if(value.equalsIgnoreCase(TESTTWO.toString())) return TestEnum.TESTTWO; else if(value.equalsIgnoreCase(NONE.toString())) return TestEnum.NONE; else return null; } }

清单文件中的权限。

答案 3 :(得分:0)

在服务类的onDestroy()方法中重新启动服务。这样,当您的应用程序关闭时,将调用onDestroy(),这将再次启动您的服务。

答案 4 :(得分:0)

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