如何显示出现在主屏幕和所有应用程序上的通知 - Android
how to show notification that shows up on main screen and all applications - Android
我想在主屏幕和任何应用程序上显示消息应用程序 Line 之类的通知,它会显示一个带有消息的通知框几秒钟,然后消失。
像这样:
那么有哪些可行的方法可以做到这一点?是定制吐司吗?或自定义对话框?
您必须像这样创建 window 动画:
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>
我的服务classxml文件:
<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 一样实现通知。
您可以使用此权限创建自定义对话框
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
关注此 answer 了解更多详情。
我想在主屏幕和任何应用程序上显示消息应用程序 Line 之类的通知,它会显示一个带有消息的通知框几秒钟,然后消失。
像这样:
那么有哪些可行的方法可以做到这一点?是定制吐司吗?或自定义对话框?
您必须像这样创建 window 动画:
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>
我的服务classxml文件:
<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 一样实现通知。
您可以使用此权限创建自定义对话框
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
关注此 answer 了解更多详情。