- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道这很蹩脚,但我是 android 开发的新手。我正在编写一个基于传感器的应用程序,每次摇动手机时都会更改墙纸。应用最小化后,它会在后台运行。
当我第一次运行它时,它运行得非常好。但是当我最小化它并重新打开它时,它看起来像应用程序的 2 个实例正在运行。所以它继续。每次我最小化并打开该应用程序时,似乎又并行启动了一个实例。
它造成的问题:
1:同一个应用程序的多个实例正在监听“摇一摇”
2: 同一应用的多个实例试图更换墙纸
3: 应用程序的最后一个实例所做的墙纸更改很明显
我试过以下设置:
android:clearTaskOnLaunch="true"
android:launchMode="singleInstance"
没有任何作用。请帮忙。
下面是我的 Activity 类和 Manifext 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="abhijit.android.sensor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:name="abhijit.android.sensor.GlobalVarible"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="abhijit.android.sensor.SensorTestActivity"
android:label="@string/app_name"
android:clearTaskOnLaunch="true"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
传感器测试 Activity .java
package abhijit.android.sensor;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SensorTestActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private boolean color = false;
private View view;
TextView text;
private long lastUpdate;
Random rnd = new Random();
int colors;
private Vibrator vibrate;
GlobalVarible globalVariable;
WallpaperManager myWallpaperManager;
Button b1,b2,b3;
private static String AppVersion ="WALL-e v0.7 (Beta)";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_test);
view = findViewById(R.id.textView);
text = (TextView) findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);
b1 = (Button) findViewById(R.id.Enable_button);
b2 = (Button) findViewById(R.id.Dis_button);
b3 = (Button) findViewById(R.id.Exit_button);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE);
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
lastUpdate = new Date().getTime();
// Calling Application class (see application tag in AndroidManifest.xml)
globalVariable = (GlobalVarible) getApplicationContext();
//Set name and email in global/application context
globalVariable.setCounter(0);
globalVariable.setWall(1);
System.out.println("Counter set to :" + globalVariable.getCounter());
}
@Override
public void onSensorChanged(SensorEvent event)
{
if ((event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) & globalVariable.appEnabled) {getAccelerometer(event);}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
// long actualTime = event.timestamp;
long actualTime = (new Date()).getTime() + (event.timestamp - System.nanoTime()) / 1000000L;
if (accelationSquareRoot >= 3) //
{
long timediff = actualTime - lastUpdate;
if (timediff < 1000) {
return;
}
// Toast.makeText(this, "Detected device movement & working !!! ", Toast.LENGTH_SHORT).show();
globalVariable.setCounter(globalVariable.getCounter()+1);
vibrate.vibrate(300);
if (color)
{
colors= Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
view.setBackgroundColor(colors);
} else {
view.setBackgroundColor(colors);
}
color = !color;
System.out.println("Counter # " + globalVariable.getCounter());
if(globalVariable.getCounter()==1)
{
globalVariable.setCounter(0);
try
{
switch (globalVariable.getWall())
{
case 1:myWallpaperManager.setResource(R.drawable.wall1);break;
case 2:myWallpaperManager.setResource(R.drawable.wall2);break;
case 3:myWallpaperManager.setResource(R.drawable.wall3);break;
case 4:myWallpaperManager.setResource(R.drawable.wall4);break;
case 5:myWallpaperManager.setResource(R.drawable.wall5);break;
case 6:myWallpaperManager.setResource(R.drawable.wall6);break;
case 7:myWallpaperManager.setResource(R.drawable.wall7);break;
case 8:myWallpaperManager.setResource(R.drawable.wall8);break;
case 9:myWallpaperManager.setResource(R.drawable.wall9);break;
case 10:myWallpaperManager.setResource(R.drawable.wall10);break;
case 11:myWallpaperManager.setResource(R.drawable.wall11);break;
case 12:myWallpaperManager.setResource(R.drawable.wall12);break;
case 13:myWallpaperManager.setResource(R.drawable.wall13);break;
case 14:myWallpaperManager.setResource(R.drawable.wall14);break;
case 15:myWallpaperManager.setResource(R.drawable.wall15);break;
case 16:myWallpaperManager.setResource(R.drawable.wall16);break;
case 17:myWallpaperManager.setResource(R.drawable.wall17);break;
case 18:myWallpaperManager.setResource(R.drawable.wall18);break;
case 19:myWallpaperManager.setResource(R.drawable.wall19);break;
case 20:myWallpaperManager.setResource(R.drawable.wall20);break;
case 21:myWallpaperManager.setResource(R.drawable.wall21);break;
case 22:myWallpaperManager.setResource(R.drawable.wall22);break;
case 23:myWallpaperManager.setResource(R.drawable.wall23);break;
case 24:myWallpaperManager.setResource(R.drawable.wall24);break;
case 25:myWallpaperManager.setResource(R.drawable.wall25);break;
case 26:myWallpaperManager.setResource(R.drawable.wall26);break;
case 27:myWallpaperManager.setResource(R.drawable.wall27);break;
default:break;
}
Toast.makeText(this, "Successfully set as wallpaper to :"+globalVariable.getWall(), Toast.LENGTH_SHORT).show();
System.out.println("Counter : Wallerpaper Set to #"+globalVariable.getWall());
globalVariable.setWall(globalVariable.getWall()+1);
if(globalVariable.getWall()>27) globalVariable.setWall(1);
lastUpdate = actualTime;
} catch (IOException e)
{Toast.makeText(this, "Error set as wallpaper :", Toast.LENGTH_SHORT).show();}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this, "Sensor detection resumed !!!", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
// sensorManager.unregisterListener(this);
Toast.makeText(this, "Sensor detection running in background !!!", Toast.LENGTH_SHORT).show();
// System.exit(0);
}
public void b1_Onclick(View v)
{
System.out.println("new Enabled!!!");
globalVariable.appEnabled=true;
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
text.setText("Application is [ ENABLED ]");
}
public void b2_Onclick(View v)
{
System.out.println("new Disbled!!!");
globalVariable.appEnabled=false;
sensorManager.unregisterListener(this);
text.setText("Application is [ DISABLED ]");
}
public void b3_Onclick(View v)
{
System.out.println("new exit!!!");
sensorManager.unregisterListener(this);
this.onPause();
this.finish();
}
public void b4_Onclick(View view)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage(AppVersion + " \n \nCopyright \u00a9 2014, \nAbhijit Mishra");
dlgAlert.setTitle(AppVersion);
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which){}});
}
}
全局变量.Java
package abhijit.android.sensor;
import android.app.Application;
public class GlobalVarible extends Application{
public int counter,wall;
boolean appEnabled=true;
public int getWall() {
return wall;
}
public void setWall(int wall) {
this.wall = wall;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
请告诉我接下来我应该做什么,以便应用程序只有 1 个线程运行,即使我将其最小化并重新启动也是如此。
问候,阿比 git
最佳答案
问题出在这里:
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
// sensorManager.unregisterListener(this);
}
您没有注销监听器,因此它被注册了多次。
我知道你想做的是即使 Activity 暂停也继续收听,你可以使用 boolean 标志只注册一次。但这可能不是一个好主意。不可见的 Activity 可以随时由系统完成。
对于这些类型的用例,Service
会更合适(作为奖励,您可以将其设置为在 BOOT_COMPLETED
上启动,这样您就不会重新启动设备时,无需重新运行应用程序来设置此监听器)。
所以,简而言之,我建议:
onCreate()
中,将服务注册为 SensorManager
的监听器(与您在此处所做的非常相似)。Intent
以启动服务(参见 docs )。关于java - 好像我的小 android 应用程序正在运行多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578280/
在引用此文档pressable docs之后,我将Pressable用于按钮 现在,我想向按钮添加波纹效果,但是它无法正常工作。 Button 如果按钮具有
在 C# 中,我想制作“智能”枚举,这在 Java 中是可能的,其中有更多信息附加到枚举值,而不仅仅是底层 int。我偶然发现了一个创建类(而不是枚举)的方案,如以下简单示例所示: public se
当执行 git stash 时,会创建 2 个提交。一个被 stash ref 引用并且有 2 个父提交。一位 parent 是我们 stash 地点的索引。另一方拥有我们 stash 的实际内容。
我是一名优秀的程序员,十分优秀!