Android屏幕未关闭
我使用此代码在收到短信时触发。一切正常,但当我在AlertDialog中单击“回复”或“关闭”时,屏幕永远不会超时,只会永远打开。我需要做些什么来给屏幕留出一定的时间Android屏幕未关闭,android,android-activity,flags,android-windowmanager,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; impor
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";
}
}
我想您可以使用
WakeLock
打开屏幕,使用DevicePolicyManager
关闭屏幕。。。我在应用程序中使用了它。。。(Wakelock不受欢迎,但工作起来很有魅力)
要关闭屏幕,请使用DevicePolicyManager
。调用dpm.lockNow()代码>当您想关闭屏幕时。
要打开scren,请使用WakeLock.acquire()代码>
否,仅关闭屏幕。比如15秒屏幕超时。我正在尝试建立一个文本消息通知弹出系统。唯一的问题是屏幕永远不会关闭。我已经可以取消对话框了。将此添加到您的清单中。
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();