启动接收器不起作用

时间:2015-05-08 12:39:36

标签: java android

首先,请不要将此标记为重复,我尝试使用SO上的几个不同问题解决问题,并尝试通过SO解决此问题的每个解决方案。

因此,我在这里与logcat一起重现我的整个代码。

问题:

我正在尝试编写一个在重启设备时启动的应用程序。 我可以看到几个应用程序在logcat中接收到BOOT_COMPLETED操作,但是我无法在设备重启的logcat中看到我的应用程序。

注意事项:

我已经在通过设备重启进行测试之前启动了我的应用程序。

代码文件:

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xyz.abc"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.xyz.abc.autostart" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".hello"
            android:label="@string/title_activity_hello" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".service"
            android:enabled="true" />
    </application>

</manifest>

Autostart.java

package com.xyz.abc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by admin on 008, 8 May 2015.
 */
public class autostart extends BroadcastReceiver
{
    public void onReceive(Context context, Intent arg1)
    {
        Log.w("boot_broadcast_poc", "starting service...");
        context.startService(new Intent(context, service.class));
    }
}

service.java

package com.xyz.abc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by admin on 008, 8 May 2015.
 */
public class service extends Service
{
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public int onStartCommand(Intent pIntent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
        Log.i("bootbroadcastpoc","NotifyingDailyService");

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

hello.java

package com.xyz.abc;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class hello extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_hello, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最后,通过BOOT_COMPLETED过滤的logcat: 更新:删除了没有必要的logcat (我也找到了我将在短期内发布的解决方案。)

  1. “boot_broadcast_poc”的logcat过滤为空白。
  2. “bootbroadcastpoc”的logcat过滤为空白。
  3. 我肯定没有看到服务已启动,因为我在启动时没有在屏幕上看到任何Toast。

1 个答案:

答案 0 :(得分:2)

正如评论中所述,我正在测试Redmi 1s,这是一款运行MIUI的小米手机。 我在模拟器上测试了这个相同的代码(准确地说是Bluestacks),它就像一个魅力。这可能是没有人有解决方案的原因!这段代码没有问题!

如上所述here,MIUI(我的Android版本为4.3)需要另一组权限:

<action android:name="android.intent.action.REBOOT"/>

在清单中。

其次,MIUI似乎设置了自动启动应用程序的特殊权限 - 可在此处设置:

  

“设置&gt;应用&gt; YOUR_APP&gt;管理权限”

您需要启用“自动启动”选项。

希望能帮助使用小米的Redmi / Mi设备的人。 附:当我遇到这个问题时,我的测试设备正在运行MIUI-JHCMIBH45.0。

相关问题