锁屏上的透明对话框回复像whatsapp这样的消息

时间:2017-03-08 04:40:27

标签: java android android-studio

我正在尝试在android中创建一个锁定屏幕对话通知,如whatsapp回复

这是我试过的

public void sendNotification(String msg, Intent i, String title)
    {
        mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        String replyLabel = "Reply";
        RemoteInput remoteInput = new RemoteInput.Builder("KEY_REPLY")
                .setLabel(replyLabel)
                .build();

        PendingIntent contentIntent = PendingIntent.getActivity(context, 100, i,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
                R.drawable.ic_phone, replyLabel, contentIntent)
                .addRemoteInput(remoteInput)
                .setAllowGeneratedReplies(true)
                .build();

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                        .setContentTitle(title)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText("Content Hidden"))
                        .setContentText(msg)
                        .addAction(replyAction)
                ;

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.cancel(Constants.PUSH_ID);
        mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
}



                Intent myIntent = new Intent(MainActivity.this, NotificationActivity.class);                ApiManager.getInstance().mChatManager.sendNotification("message", myIntent, "Muhammad Umar");

为了让它显得透明,我试过了

<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.5</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:background">@android:color/transparent</item>
</style>

但是我得到了结果

enter image description here

背景是暗淡的白色,我无法将其设置为透明。我的方法是否正确?为什么活动不透明。

4 个答案:

答案 0 :(得分:0)

<style name="AppTheme.NoActionBar.Transparent">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transperent_green</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

<color name="transperent_green">#CC07D1C8</color>


<activity
    android:name=".YourActivityName"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar.Transparent"></activity>

答案 1 :(得分:0)

根据我的理解,我认为你需要的是&#34; System_Alert_Window&#34;允许。 Facebook,viber使用此权限打开一个对话框,活动浮动/通过另一个应用程序。检查此SO link。希望有所帮助。

答案 2 :(得分:0)

有一种更好的方法。将Activity设为Dialog,然后使其背景透明。

您可以通过在运行时使Dialog的背景透明来实现此目的。

NotificationDialog dialog = new NotificationDialog(MainActivity.class);
dialog .getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(MainActivity.this, android.R.color.transparent)));
dialog.show();

<强>更新

使用“活动”,将NotificationActivity的主题从@style/Theme.AppCompat.Dialog更改为Manifest。现在,您的活动将显示为对话框。您可以根据需要进行编辑。像

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //If you don't want to show title
        //Use requestWindowFeature(Window.FEATURE_NO_TITLE) if you are extending Activity
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_notification);

        //Play with this if you want to change  the width and height of your `Dialog`
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;

        getWindow().setLayout(width-20, height-100);

    }
}

答案 3 :(得分:0)

简单地在mainfiest中将它添加到一个稀释盒式主题中。

        <activity
        android:name="com.and.your activity"
        android:theme="@android:style/Theme.Dialog" >
            </activity>