在WAKE_LOCK-ED活动下,TextView setText不起作用

时间:2015-07-17 06:35:42

标签: android textview settext

我有一个警报,在一段时间后使用某个活动启动。此活动显示在WAKE_LOCK函数下。现在,我希望活动显示警报的信息。我使用TextViews来显示Activity的onCreate中的信息。由于某种原因,这个测试代码不起作用。

DataGridView1.CurrentCell = DataGridView1.Rows(selRow).Cells(selCol)

你们有没有想过它为什么不起作用?

activity_screen XML:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);

    TextView tvName = (TextView) findViewById(R.id.alarm_screen_name);
    tvName.setText("123");


    Button dismissButton = (Button) findViewById(R.id.reminderDismiss);

    dismissButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            finish();
        }
    });
    //Ensure wakelock release
    Runnable releaseWakelock = new Runnable() {

        @Override
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

            if (mWakeLock != null && mWakeLock.isHeld()) {
                mWakeLock.release();
                finish();
            }
        }
    };
    new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}

活动从此WAKE_LOCK显示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.ScreenActivity">

<TextView
    android:id="@+id/alarm_screen_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="Alarm!"
    android:textSize="38dp" />

<TextView
    android:id="@+id/alarm_screen_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="00 : 00"
    android:textSize="52dp" />

<TextView
    android:id="@+id/alarm_screen_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="Alarm name" />

<Button
    android:id="@+id/reminderDismiss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="106dp"
    android:text="Dismiss"
    android:onClick="dismiss"
    android:textSize="38dp" />

该服务在此按钮下从另一个活动激活:

public class AlarmService extends Activity {
private PowerManager.WakeLock mWakeLock;



public void onCreate(Bundle savedInstateState) {
    super.onCreate(savedInstateState);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Wake Log");
    mWakeLock.acquire();
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_screen);


    //stop alarm
    Button stopAlarm = (Button) findViewById(R.id.reminderDismiss);
    stopAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}
}

1 个答案:

答案 0 :(得分:0)

这对我来说很合适,请点击这里的屏幕截图enter image description here

我写的代码与你在这里看到的相同

public class MainActivity extends Activity  {
    TextView tvText;
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tvText=(TextView)findViewById(R.id.alarm_screen_name);
         tvText.setText("123");

    }

      }