【问题标题】:how to show notification that shows up on main screen and all applications - Android如何显示显示在主屏幕和所有应用程序上的通知 - Android
【发布时间】:2015-10-07 23:27:35
【问题描述】:

我想在主屏幕和任何应用程序上显示消息应用程序 Line 之类的通知,它会在其中显示带有消息的通知框几秒钟然后消失。

类似这样的:

那么有什么可能的方法来做到这一点?它是自定义吐司吗?还是自定义对话框?

【问题讨论】:

    标签: android android-notifications android-toast


    【解决方案1】:

    你必须像这样创建一个窗口动画:

    MyService.java

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.PixelFormat;
    import android.os.IBinder;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    
    /**
     * Created by darshan.mistry on 7/18/2015.
     */
    public class MyService extends Service {
    
        View mView;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            // instance of WindowManager
            WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    
            LayoutInflater mInflater = (LayoutInflater)
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            // inflate required layout file
            mView = mInflater.inflate(R.layout.abc, null);
    
            // attach OnClickListener
            mView.findViewById(R.id.someView).setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // you can fire an Intent accordingly - to deal with the click event
                    // stop the service - this also removes `mView` from the window
                    // because onDestroy() is called - that's where we remove `mView`
                    stopSelf();
                }
            });
    
            // the LayoutParams for `mView`
            // main attraction here is `TYPE_SYSTEM_ERROR`
            // as you noted above, `TYPE_SYSTEM_ALERT` does not work on the lockscreen
            // `TYPE_SYSTEM_OVERLAY` works very well but is focusable - no click events
            // `TYPE_SYSTEM_ERROR` supports all these requirements
            WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                    PixelFormat.RGBA_8888);
    
            // finally, add the view to window
            mWindowManager.addView(mView, mLayoutParams);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            // remove `mView` from the window
            removeNow();
        }
    
    
    
        // Removes `mView` from the window
        public void removeNow() {
            if (mView != null) {
                WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
                wm.removeView(mView);
            }
        }
    }
    

    MyReceiver.java

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    /**
     * Created by darshan.mistry on 7/18/2015.
     */
    public class MyReciver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent intent1 = new Intent(context, MyService.class);
            context.startService(intent1);
        }
    }
    

    在清单文件中,添加权限:

    <!-- [My service and reciver] -->
            <service
                android:name=".MyService"
                android:exported="true" />
            <receiver android:name=".MyReciver">
                <intent-filter>
                    <action android:name="com.tutorialspoint.CUSTOM_INTENT"></action>
                </intent-filter>
            </receiver>
    

    MyService 类 xml 文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="@android:color/white">
    
        <TextView
            android:id="@+id/someView"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:background="@android:color/white"
            android:gravity="center"
            android:text="This is Demo"
            android:textColor="@android:color/black" />
    
    </RelativeLayout>
    

    您必须从收到通知的地方调用 MyService 添加以下行:

     Intent intent = new Intent();
            intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
            sendBroadcast(intent);
    

    这是一个简单的示例,因此您对如何实现类似于 line app 的通知有所了解。

    【讨论】:

      【解决方案2】:

      您可以使用此权限创建自定义对话框

      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
      

      关注answer了解更多详情。

      【讨论】:

        猜你喜欢
        • 2011-11-18
        • 2012-07-24
        • 2016-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多