Вы находитесь на странице: 1из 12

Praktikum -- : NotificationManager

Package : com.user.notificationmanager.util

SystemUiHider.java

package com.qiqi.notificationmanager.util;

import android.app.Activity;
import android.os.Build;
import android.view.View;

public abstract class SystemUiHider {


public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1;

public static final int FLAG_FULLSCREEN = 0x2;

public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4;

protected Activity mActivity;

protected View mAnchorView;

protected int mFlags;

protected OnVisibilityChangeListener mOnVisibilityChangeListener =


sDummyListener;

public static SystemUiHider getInstance(Activity activity, View


anchorView,
int flags) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return new SystemUiHiderHoneycomb(activity, anchorView,
flags);
} else {
return new SystemUiHiderBase(activity, anchorView,
flags);
}
}

protected SystemUiHider(Activity activity, View anchorView, int


flags) {
mActivity = activity;
mAnchorView = anchorView;
mFlags = flags;
}

public abstract void setup();

public abstract boolean isVisible();

public abstract void hide();

public abstract void show();

public void toggle() {


if (isVisible()) {
hide();
} else {
show();
}
}

public void setOnVisibilityChangeListener(


OnVisibilityChangeListener listener) {
if (listener == null) {
listener = sDummyListener;
}

mOnVisibilityChangeListener = listener;
}

private static OnVisibilityChangeListener sDummyListener = new


OnVisibilityChangeListener() {
@Override
public void onVisibilityChange(boolean visible) {
}
};

public interface OnVisibilityChangeListener {


public void onVisibilityChange(boolean visible);
}
}

SystemUiHiderBase.java

package com.qiqi.notificationmanager.util;

import android.app.Activity;
import android.view.View;
import android.view.WindowManager;

public class SystemUiHiderBase extends SystemUiHider {


private boolean mVisible = true;

protected SystemUiHiderBase(Activity activity, View anchorView, int


flags) {
super(activity, anchorView, flags);
}

@Override
public void setup() {
if ((mFlags & FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES) == 0) {
mActivity.getWindow().setFlags(

WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
|
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,

WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
|
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
@Override
public boolean isVisible() {
return mVisible;
}

@Override
public void hide() {
if ((mFlags & FLAG_FULLSCREEN) != 0) {
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mOnVisibilityChangeListener.onVisibilityChange(false);
mVisible = false;
}

@Override
public void show() {
if ((mFlags & FLAG_FULLSCREEN) != 0) {
mActivity.getWindow().setFlags(0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mOnVisibilityChangeListener.onVisibilityChange(true);
mVisible = true;
}
}

SystemUiHiderHoneycomb.java

package com.qiqi.notificationmanager.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SystemUiHiderHoneycomb extends SystemUiHiderBase {
private int mShowFlags;

private int mHideFlags;

private int mTestFlags;

private boolean mVisible = true;

protected SystemUiHiderHoneycomb(Activity activity, View anchorView,


int flags) {
super(activity, anchorView, flags);

mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE;
mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if ((mFlags & FLAG_FULLSCREEN) != 0) {
mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}

if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) {


mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
mTestFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
}

@Override
public void setup() {
mAnchorView
.setOnSystemUiVisibilityChangeListener(mSystemUiVis
ibilityChangeListener);
}

/** {@inheritDoc} */
@Override
public void hide() {
mAnchorView.setSystemUiVisibility(mHideFlags);
}

/** {@inheritDoc} */
@Override
public void show() {
mAnchorView.setSystemUiVisibility(mShowFlags);
}

/** {@inheritDoc} */
@Override
public boolean isVisible() {
return mVisible;
}

private View.OnSystemUiVisibilityChangeListener
mSystemUiVisibilityChangeListener = new
View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int vis) {
if ((vis & mTestFlags) != 0) {
if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN) {
mActivity.getActionBar().hide();
mActivity.getWindow().setFlags(

WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

mOnVisibilityChangeListener.onVisibilityChange(false);
mVisible = false;

} else {
mAnchorView.setSystemUiVisibility(mShowFlags);
if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN) {
mActivity.getActionBar().show();
mActivity.getWindow().setFlags(0,

WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

mOnVisibilityChangeListener.onVisibilityChange(true);
mVisible = true;
}
}
};
}

Package : com.user.notificationmanager

FullScreenActivity.java

package com.qiqi.notificationmanager;

import com.qiqi.notificationmanager.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

public class FullscreenActivity extends Activity {


private static final boolean AUTO_HIDE = true;

private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

private static final boolean TOGGLE_ON_CLICK = true;

private static final int HIDER_FLAGS =


SystemUiHider.FLAG_HIDE_NAVIGATION;

private SystemUiHider mSystemUiHider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_fullscreen);

final View controlsView =


findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
mSystemUiHider = SystemUiHider.getInstance(this, contentView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new
SystemUiHider.OnVisibilityChangeListener() {
int mControlsHeight;
int mShortAnimTime;

@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean
visible) {
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.HONEYCOMB_MR2) {
if (mControlsHeight == 0) {
mControlsHeight =
controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime =
getResources().getInteger(

android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible
? 0 : mControlsHeight)
.setDuration(mShortAn
imTime);
} else {

controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}

if (visible && AUTO_HIDE) {

delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});

contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});

findViewById(R.id.dummy_button).setOnTouchListener(
mDelayHideTouchListener);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

delayedHide(100);
}

View.OnTouchListener mDelayHideTouchListener = new


View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};

Handler mHideHandler = new Handler();


Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mSystemUiHider.hide();
}
};

private void delayedHide(int delayMillis) {


mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}

CreateNotificationActivity.java

package com.qiqi.notificationmanager;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class CreateNotificationActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void createNotification(View view) {


Intent intent = new Intent();
intent.setAction("com.user.CUSTOM_INTENT");
intent.putExtra("todo", "play");
PendingIntent pIntent = PendingIntent.getBroadcast
(this.getBaseContext(), 0, intent, 0);
Intent intent1 = new Intent();
intent1.setAction("com.user.CUSTOM_INTENT_NEW");
intent1.putExtra("todo", "pause");
PendingIntent pIntent1 = PendingIntent.getBroadcast
(this.getBaseContext(), 0, intent1, 0);

Notification noti = new Notification.Builder(this)


.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent1).build();
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);
}

NotificationReceiverActivity.java

package com.qiqi.notificationmanager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class NotificationReceiverActivity extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = extras.getString("todo");
Toast.makeText(context, message, 5000).show();

if(message!=null){
todo(context, message);
}
}

public void todo(Context context, String todo){


if(todo.equals("play")){
Toast.makeText(context, todo+": Playing", 5000).show();

}
if(todo.equals("pause")){
Toast.makeText(context, todo+": Pausing", 5000).show();

}
}

}
Hasil.java

package com.qiqi.notificationmanager;

import android.app.Activity;
import android.os.Bundle;

public class Hasil extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hasil);
}
}

Layout

activity_fullscreen.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.qiqi.notificationmanager.FullscreenActivity" >

<!--
The primary full-screen view. This can be replaced with whatever
view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->

<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />

<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >

<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>

</FrameLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="createNotification"
android:text="Create Notification" >
</Button>

</LinearLayout>

result.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the result activity opened from the
notification" >
</TextView>

</LinearLayout>

hasil.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Notification More"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiqi.notificationmanager"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CreateNotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity
android:name=".Hasil"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<receiver
android:name="com.qiqi.notificationmanager.NotificationReceiverActivity">
<intent-filter>
<action android:name="com.qiqi.CUSTOM_INTENT">
</action>
<action android:name="com.qiqi.CUSTOM_INTENT_NEW">
</action>
</intent-filter>
</receiver>
</application>

</manifest>

Вам также может понравиться