如何显示主屏幕和所有应用程序上显示的通知 - Android

MBH*_*MBH 1 android android-notifications android-toast

我想在主屏幕和任何应用程序上显示通知,如消息应用程序Line,它会显示一个通知框,消息在其中显示一秒钟然后消失.

这样的事情:

例

那么有什么方法可以做到这一点?这是一个定制的吐司吗?或自定义对话框?

Dar*_*try 5

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<!-- [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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

 Intent intent = new Intent();
        intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
        sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

这是一个简单的示例,因此您可以了解如何实现线应用等通知.