Android Alarm Manager和接收器无法在API 17仿真器中运行

时间:2016-06-15 06:01:03

标签: android android-emulator broadcastreceiver alarmmanager android-4.2-jelly-bean

嗯,这很有意思。我的警报在API 19,20,23等其他Android版本中运行正常,但在API 17中无效。我尝试了许多不同的方法,但它不适用于任何使用API​​ 17的模拟器。

以下是示例代码。它是一个基本的警报,每隔10秒显示一次Toast消息。它在其他版本上正常工作。 (由于警报在Android 5.1之后的版本中不短于60秒,因此在使用API​​ 22和23的设备上每分钟显示消息而不是10秒,如预期的那样)但是,使用API​​ 17的模拟器上不会显示消息。甚至onReceive ()方法没有调用BroadcastReceiver。

以下是代码:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sezai.basicalarm">
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:process=":remote" android:name=".Alarm"/>
        <service
            android:name=".MyService"
            android:enabled="true"
            android:process=":my_service" >
        </service>
    </application>
</manifest>

服务

public class MyService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        alarm.SetAlarm(this);
        Log.i("Service:", "Alarm set!");
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        alarm.SetAlarm(this);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

广播接收器:

public class Alarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Put here YOUR code.
        Toast.makeText(context, "Alarm!", Toast.LENGTH_LONG).show(); // For example
        Log.i("Alarm", "Showed the toast!");
        wl.release();
    }

    public void SetAlarm(Context context)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pi); // Millisec * Second * Minute
    }

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent mIntent = new Intent(this, MyService.class);
        startService(mIntent);

        final Context context = this;
    }

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

我尝试了不同的仿真器,如Nexus 4,Nexus 7,Galaxy Nexus。每次使用API​​ 17时,代码都不起作用。它在使用不同API的模拟器上正常工作。

我在win7 pro上使用Android Studio 2.1.2。所有组件(如AVD Manager,SDK Manager)都是最新的。

那么,它只是我还是像虫子一样?

0 个答案:

没有答案