0

我正在使用此代码在收到短信时触发。一切正常,但是当我从 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";

    }

}
4

1 回答 1

2

I think you can use WakeLock for turning on and DevicePolicyManager for turning off the screen... I used it in my application... (Wakelock is deprecated but working like a charm).

To turn off the screen use DevicePolicyManager. Call dpm.lockNow(); when you want to turn the screen off. To turn on the scren use 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();
于 2014-08-25T06:48:38.193 回答