我有一个Activity类,其中我有一个静态的标志,让我们说
公共静态挥发标志= FALSE;
然后在上课,我启动一个线程,它检查标志,并做不同的事情。
我也有一个BroadcastReceiver,它将该标志设置为true或false。
我虽然波动将迫使该标志到最近的值。但我可以看到我的BroadcastReceiver设置静态标志为真,但我的线程仍然得到它为假。
我在这里丢失基本的东西?任何帮助将是AP preciated!
简体code(更新) - 这样的标志应该是在一分钟后更改为正确的。但它从来没有。但是,从广播接收器消息显示它已被更改为True
TestappActivity.java:
com.test包;进口的java.util.Calendar;进口android.app.Activity;进口android.app.AlarmManager;进口android.app.PendingIntent;进口android.content.Intent;进口android.os.Bundle;公共类TestappActivity延伸活动{ / **当第一次创建活动调用。 * / @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 意图intent0 =新意图(这一点,TestService.class); this.startService(intent0); 意向意图=新意图(这一点,TestReceiver.class); AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE); 发件人的PendingIntent = PendingIntent.getBroadcast(这一点, 1,意向, PendingIntent.FLAG_UPDATE_CURRENT); 日历插槽= Calendar.getInstance(); INT分钟= slot.get(Calendar.MINUTE); slot.set(Calendar.MINUTE,MIN + 1); am.set(AlarmManager.RTC_WAKEUP,slot.getTimeInMillis(),发送者); }}
TestService.java:
com.test包;进口android.app.Service;进口android.content.Intent;进口android.os.IBinder;进口android.util.Log;公共类TestService的扩展服务{ 私有静态最后弦乐TAG =TestService的; 公共静态挥发布尔标志= FALSE; 私人MyTopThread mTopThread; 公共TestService的(){ } @覆盖 公众的IBinder onBind(意向意图){ 返回null; } @覆盖 公共无效的onCreate(){ } @覆盖 公共无效的onDestroy(){ } @覆盖 公众诠释onStartCommand(意向意图,诠释标志诠释startId){ 保护(); //我们希望该服务继续运行,直到它被明确 //停止,因此返回粘。 返回START_STICKY; } / ** *运行保护 * * / 私人无效的保护(){ mTopThread =新MyTopThread(); mTopThread.start(); } 私有类MyTopThread继承Thread { @覆盖 公共无效的run(){ 而(真){ 尝试{ 视频下载(150); Log.d(TAG,标志为+ TestService.flag); }赶上(InterruptedException的E){ e.printStackTrace(); } } } }}
TestReceiver.java:
com.test包;进口android.content.BroadcastReceiver;进口android.content.Context;进口android.content.Intent;进口android.util.Log;公共类TestReceiver扩展广播接收器{ 最终静态私人字符串TAG =TestReceiver; @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ Log.d(TAG的onReceive被触发...); TestService.flag = TRUE; Log.d(TAG,标志被改变为+ TestService.flag); }}
的AndroidManifest.xml:
<?XML版本=1.0编码=UTF-8&GT?;<清单的xmlns:机器人=http://schemas.android.com/apk/res/android 包=com.test 安卓版code =1 机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =8/> <应用 机器人:图标=@绘制/ ic_launcher 机器人:标签=@字符串/ APP_NAME> <活动 机器人:名字=。TestappActivity 机器人:标签=@字符串/ APP_NAME> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <服务机器人:名字=。TestService的/> <接收 机器人:名字=。TestReceiver 机器人:工艺=:远程> < /接收器> < /用途>< /清单>
解决方案
我认为问题是,你正在运行在自己的进程接收器。从文档的安卓的
:&LT过程
属性;接收器GT;
如果分配给该属性名称以冒号开始(':'),一个新的进程,私有的应用程序,是当需要它和广播接收器在运行过程中创建的
我想接收器正在修改的过程中,本地版本 TestService.flag
,正在使用的 TestService的。请尝试删除
的android:从工艺
属性<接收>
标签在你的清单
I have an Activity class, in which I have a static flag, let's say
public static volatile flag = false;
Then in the class, I start a thread, which checks the flag and do different things.
I also have a broadcastreceiver, which sets the flag to true or false.
I though volatile will force the flag to the most recent value. But I can see my broadcastreceiver sets the static flag to true, but my thread is still getting it as false.
Am I missing something basic here? Any help would be appreciated!
Simplified Code (Updated) - So the flag is supposed to change to true after one minute. But it never did. But message from broadcast receiver shows it has been change to true
TestappActivity.java:
package com.test;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class TestappActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent0 = new Intent(this, TestService.class);
this.startService(intent0);
Intent intent = new Intent(this, TestReceiver.class);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent sender = PendingIntent.getBroadcast(this,
1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar slot = Calendar.getInstance();
int min = slot.get(Calendar.MINUTE);
slot.set(Calendar.MINUTE, min+1);
am.set(AlarmManager.RTC_WAKEUP, slot.getTimeInMillis(), sender);
}
}
TestService.java:
package com.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service {
private static final String TAG = "TestService";
public static volatile boolean flag = false;
private MyTopThread mTopThread;
public TestService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
}
@Override
public void onDestroy() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
protect();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
/**
* Run protection
*
*/
private void protect() {
mTopThread = new MyTopThread();
mTopThread.start();
}
private class MyTopThread extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(150);
Log.d(TAG, "Flag is " + TestService.flag);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
TestReceiver.java:
package com.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class TestReceiver extends BroadcastReceiver {
final static private String TAG = "TestReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive is triggered ...");
TestService.flag = true;
Log.d(TAG, "flag is changed to " + TestService.flag);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestappActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TestService" />
<receiver
android:name=".TestReceiver"
android:process=":remote" >
</receiver>
</application>
</manifest>
解决方案
I think the problem is that you are running the receiver in its own process. From the docs for the android:process
attribute of <receiver>
:
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the broadcast receiver runs in that process.
I think the receiver is modifying a process-local version of TestService.flag
, not the one being used by TestService
. Try removing the android:process
attribute from the <receiver>
tag in your manifest.