java – 让Android上的应用程序始终在Android中运行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 让Android上的应用程序始终在Android中运行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5721字,纯文字阅读大概需要9分钟。
内容图文
![java – 让Android上的应用程序始终在Android中运行](/upload/InfoBanner/zyjiaocheng/704/1309b217588b494681f95efa015412d2.jpg)
我必须得到人们的轨迹(例如,从家到工作),所以我的应用程序获得经度和经度(我有两个按钮:1.开始获得lat和lon 2.停止获得lat和lon).但是,我希望android不会杀死应用程序,我希望保持我的应用程序运行,而用户使用Facebook,Twitter(例如)或只是用户锁定他的智能手机.
当用户在使用Facebook或推特(例如)时使用应用程序时,我的应用程序正常工作,但当我锁定我的智能手机时,Android会杀死应用程序.
我试图使用intentservice和service,但是当我锁定屏幕时它们不起作用.我应该使用PowerManager.WakeLock吗?我不知道它是如何工作的以及它的作用.
这是我尝试服务的一个例子,我不知道我是不是错了,但它在以下情况下不起作用:
1.我在应用程序中(我已启动该服务)
2.我点击主页按钮(服务正在运行时)
我锁屏.
然后服务和应用程序被android杀死(并且服务没有完成它的东西)
表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javosoft.intentservice">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:exported="false"></service>
</application>
</manifest>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.javosoft.intentservice.MainActivity">
<Button
android:text="start Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:id="@+id/button"
android:onClick="startService"/>
<Button
android:text="Stop Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginBottom="68dp"
android:id="@+id/button2"
android:onClick="stopService" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="textito :D"
android:ems="10"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/editText" />
</RelativeLayout>
主要活动
package com.javosoft.intentservice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
}
public void stopService (View view){
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}
MyService类
package com.javosoft.intentservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
final class MyThreadClass implements Runnable{
int service_id;
MyThreadClass(int service_id){
this.service_id = service_id;
}
@Override
public void run() {
synchronized (this){
int count = 0;
while (count < 10){
Log.i("servicio", "while: " + String.valueOf(count));
try {
wait(2000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
stopSelf(service_id);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "El servició ha empezado D:", Toast.LENGTH_SHORT).show();
Log.i("servicio", "El servició ha empezado D:");
Log.i("servicio", "onStartCommand arriba");
Thread thread = new Thread(new MyThreadClass(startId));
thread.start();
Log.i("servicio", "onStartCommand abajo");
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//super.onDestroy();
Toast.makeText(this, "El servicio ha padarado ._.", Toast.LENGTH_SHORT).show();
Log.i("servicio", "El servicio ha padarado ._.");
}
//@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
解决方法:
试试我们的这个
public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Your logical code here
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
//When remove app from background then start it again
startService(new Intent(this, ForegroundService.class));
super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
// Used only in case of bound services.
return null;
}
}
在开始按钮上单击:
Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
startService(startIntent);
在清单中
<service
android:name=".ForegroundService"
android:enabled="true"
android:stopWithTask="false" />
内容总结
以上是互联网集市为您收集整理的java – 让Android上的应用程序始终在Android中运行全部内容,希望文章能够帮你解决java – 让Android上的应用程序始终在Android中运行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。