如何在用户打开系统设置时获取事件

时间:2014-02-03 12:06:01

标签: android

我想启动我的活动wnen用户打开设备设置。有没有办法找到系统设置正在打开?

1 个答案:

答案 0 :(得分:2)

是的,在您的应用上使用Service或AlarmManager。可以检查是否正在运行应用是系统设置?

将其放入您的代码中:

try {

            Intent intent1 = new Intent(thisActivity, AlarmReciever.class);
            Apps.sender = PendingIntent.getBroadcast(thisActivity, 2, intent1,
                    0);
            Apps.am = (AlarmManager) getSystemService(ALARM_SERVICE);
            long l = new Date().getTime();
            if (l < new Date().getTime()) {
                l += 100;
            }
            Apps.am.setRepeating(AlarmManager.RTC_WAKEUP, l, 100, sender);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_SHORT).show();
        }

将此权限添加到清单:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

并在之前添加:

<receiver android:name="com.example.MBRMOrion.AlarmReciever" />

将类创建为:

public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ActivityManager am1 = (ActivityManager) thisActivity
            .getSystemService(Activity.ACTIVITY_SERVICE);
    String packageName = am1.getRunningTasks(1).get(0).topActivity
            .getPackageName(); //running app.
}

}