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