com.commonsware.android.remoteinput.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.commonsware.android.remoteinput.MainActivity.java

Source

/***
  Copyright (c) 2014-2016 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.
      
  Covered in detail in the book _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
 */

package com.commonsware.android.remoteinput;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = new Intent(this, RemoteInputReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteInput remoteInput = new RemoteInput.Builder(RemoteInputReceiver.EXTRA_INPUT)
                .setLabel(getString(R.string.talk)).build();

        NotificationCompat.Action remoteAction = new NotificationCompat.Action.Builder(
                android.R.drawable.ic_btn_speak_now, getString(R.string.talk), pi).addRemoteInput(remoteInput)
                        .build();

        NotificationCompat.Builder builder = RemoteInputReceiver.buildNotificationBase(this)
                .addAction(remoteAction);

        NotificationManagerCompat.from(this).notify(RemoteInputReceiver.NOTIFY_ID, builder.build());

        finish();
    }
}