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