Android CWAC定位轮询器在按下home(主页)按钮后关闭
我正在运行一个cwac locpoll的演示示例。这意味着在LocationLog.txt文件中记录我每分钟的位置。只要应用程序在屏幕上,它就可以正常工作,但如果我按下home按钮,它就会停止日志记录,当我返回应用程序时,它会再次重新加载。当我在屏幕上运行时锁定屏幕时,也发生了同样的事情,但我强迫应用程序与以下对象睡觉:Android CWAC定位轮询器在按下home(主页)按钮后关闭,android,android-location,commonsware-cwac,android-background,commonsware,Android,Android Location,Commonsware Cwac,Android Background,Commonsware,我正在运行一个cwac locpoll的演示示例。这意味着在LocationLog.txt文件中记录我每分钟的位置。只要应用程序在屏幕上,它就可以正常工作,但如果我按下home按钮,它就会停止日志记录,当我返回应用程序时,它会再次重新加载。当我在屏幕上运行时锁定屏幕时,也发生了同样的事情,但我强迫应用程序与以下对象睡觉: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow(
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
然而,这并没有解决我在设备上运行其他应用程序时在后台运行的问题。我使用的设备是华为荣誉7
我的主要活动的全部代码如下:
public class MainActivity extends AppCompatActivity {
private static final int PERIOD= 1000 * 60 * 1; // 1 minute
private PendingIntent pi=null;
private AlarmManager mgr=null;
PowerManager pm;
PowerManager.WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView test = (TextView) findViewById(R.id.testTV);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
mgr=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent i=new Intent(this, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT,
new Intent(this, LocationReceiver.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER,
LocationManager.GPS_PROVIDER);
pi=PendingIntent.getBroadcast(this, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
PERIOD,
pi);
Toast
.makeText(this,
"Location polling every 1 minutes begun",
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_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);
}
}我的问题很容易解决。在您的设置中搜索受保护的应用程序,找到您的应用程序并将其标记为受保护。它允许你的应用程序在后台运行。否则,华为的任务管理器将继续扼杀它。它似乎在LG L7 II上工作,那么华为有什么问题?