Android屏幕未关闭

时间:2012-05-21 02:22:57

标签: android android-activity flags android-windowmanager

我正在使用此代码在收到短信时触发。一切正常,但是当我点击AlertDialog中的“回复”或“关闭”时,屏幕永远不会超时,只是永远地说出来。我需要做些什么才能让屏幕显示一定的时间?

package package.name.here;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

public class ReceivingSMSActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        Intent in = this.getIntent();
        String sender = getIntent().getStringExtra("sender");
        String body = getIntent().getStringExtra("body");
        long timestamp = getIntent().getLongExtra("timestamp", 0L);
        SMSMessage msg = (SMSMessage) in.getSerializableExtra("msg");

        if (msg == null) {
            msg = new SMSMessage();
            msg.setPhone("0123456789");
            msg.setTimestamp(System.currentTimeMillis());
            msg.setBody("Sample Text Message");
        } else {
            msg = new SMSMessage();
            msg.setPhone(sender);
            msg.setTimestamp(timestamp);
            msg.setBody(body);
        }

        showDialog(msg);

    }

    private void showDialog(SMSMessage msg) {

        final String sender = msg.getPhone();
        final String body = msg.getBody();

        LayoutInflater factory = LayoutInflater.from(this);            
        final View alertView = factory.inflate(R.layout.main, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(alertView)
        .setTitle(getContactName(sender))
        .setCancelable(false)
        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                goHome();               
            }
        })
        .setPositiveButton("Reply", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                smsReply(sender);               
            }
        });

        AlertDialog alert = builder.create();

        TextView t = (TextView) alertView.findViewById(R.id.alertDialog); 
        t.setText(body);

        alert.show();

    }

    private void smsReply(String sender) {

        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("address", sender);
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, 0);

        this.finish();

    }

    private void goHome() {
        this.finish();
    }

    private String getContactName(String number) {

        Cursor managedCursor = managedQuery(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)),
                new String[]{PhoneLookup.DISPLAY_NAME},
                null, null, null);

        if (managedCursor.moveToFirst()) {
                //Log.d("SmsReceiver", "From: " + managedCursor.getString(managedCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)));
            return managedCursor.getString(managedCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)) + "\n";
        }
        return number + "\n";

    }

}

1 个答案:

答案 0 :(得分:2)

我认为您可以使用WakeLock打开屏幕并DevicePolicyManager关闭屏幕...我在我的应用程序中使用它...(Wakelock已被弃用,但工作就像一个魅力)。

要关闭屏幕,请使用DevicePolicyManager。如果要关闭屏幕,请致电dpm.lockNow();。 要开启scren,请使用WakeLock.acquire();

private static final String desc = "Some Description About Your Admin";
private static final int ADMIN_INTENT = 15;
private DevicePolicyManager dpm;
private ComponentName cname;
dpm = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
cname = new ComponentName(getApplicationContext(),Receiver.class); /* Receiver Class Extends Device Admin Receiver*/

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cname);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,desc);
startActivityForResult(intent, ADMIN_INTENT);    

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                                     | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                     | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();