Android volatile not working?

I have an Activity class in which I have a static flag, say

public static volatile flag = false;

Then in the class, I run a thread that checks the flag and does different things.

I also have a broadcast receiver that sets the flag to true or false.

I, although volatile, will force the flag to use the last value. But I see that my broadcast passes the static flag to true, but my thread still perceives it as false.

Did I miss something basic here? Any help would be appreciated!

Simplified code (updated) - means the flag should change to true after one minute. But that never happened. But the message from the broadcast receiver shows that it has changed 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>
+5
3

, , . android:process <receiver>:

, , (':'), , , , , .

, TestService.flag, , TestService. android:process <receiver> .

+7

I really hope your service flow will not be this (I don't see another):

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();
            }

        }
    }

}

Because you have it while(true)here, not while(!flag)as it should be.

0
source

All Articles