Notification使用详解

Notification和Toast一样,都是在android中起到通知和提醒的功能。但他们的使用原理完全不同,Toast是一个控件,使用new创建,而Notification是通过NotificationManager来管理的。

显示效果如下:


主程序:

public class Main extends Activity implements OnClickListener
{
	private NotificationManager notificationManager;
	

	private void setDefaults(String tickerText, String contentTitle,
			String contentText, int id, int resId, int defaults)
	{
		Notification notification = new Notification(resId,
				tickerText, System.currentTimeMillis());
		//创建Notification第三步
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, Main.class), 0);

		//创建Notification第四步
		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentIntent);
		notification.defaults = defaults;
		//创建Notification第五步
		notificationManager.notify(id, notification);
		

	}

	private void showNotification(String tickerText, String contentTitle,
			String contentText, int id, int resId)
	{
		//创建Notification第二步
		Notification notification = new Notification(resId,
				tickerText, System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				getIntent(), 0);

		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentIntent);
		notificationManager.notify(id, notification);

	}

	public void onClick(View v)
	{

		switch (v.getId())
		{
			case R.id.btnSmile:
				showNotification("今天非常高兴", "今天考试得了全年级第一",
						"数学100分、语文99分、英语100分,yeah!", R.drawable.smile,
						R.drawable.smile);
				break;

			case R.id.btnWhy:
				showNotification("这是为什么呢?", "这道题为什么会出错呢?", "谁有正确答案啊.",
						R.drawable.why, R.drawable.why);
				break;
			case R.id.btnWrath:
				showNotification("今天心情不好", "也不知道为什么,这几天一直很郁闷.", "也许应该去公园散心了",
						R.drawable.why, R.drawable.wrath);
				break;
			case R.id.btnClear:
				// notificationManager.cancel(R.drawable.smile);
				// notificationManager.cancel(R.drawable.why);
				notificationManager.cancelAll();
				break;
			case R.id.btnRing:
				setDefaults("使用默认的声音", "使用默认的声音", "使用默认的声音", R.id.btnRing, R.drawable.smile,
						Notification.DEFAULT_SOUND);
			case R.id.btnVibrate:
				setDefaults("使用默认的震动", "使用默认的震动", "使用默认的震动", R.id.btnVibrate,
						R.drawable.smile, Notification.DEFAULT_VIBRATE);
			case R.id.btnLight:
				setDefaults("使用默认的Light", "使用默认的Light", "使用默认的Light", R.id.btnLight,
						R.drawable.smile, Notification.DEFAULT_LIGHTS);
			case R.id.btnRingAndVibrate:
				setDefaults("所有的都使用默认值", "所有的都使用默认值", "所有的都使用默认值",
						R.id.btnRingAndVibrate, R.drawable.smile,
						Notification.DEFAULT_ALL);
			
				break;

		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//创建Notification第一步
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Button btnSmile = (Button) findViewById(R.id.btnSmile);
		Button btnWhy = (Button) findViewById(R.id.btnWhy);
		Button btnWrath = (Button) findViewById(R.id.btnWrath);
		Button btnClear = (Button) findViewById(R.id.btnClear);
		Button btnRing = (Button) findViewById(R.id.btnRing);		
		Button btnVibrate = (Button) findViewById(R.id.btnVibrate);
		Button btnLight = (Button) findViewById(R.id.btnLight);
		Button btnRingAndVibrate = (Button) findViewById(R.id.btnRingAndVibrate);
		btnSmile.setOnClickListener(this);
		btnWhy.setOnClickListener(this);
		btnWrath.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		btnRing.setOnClickListener(this);
		btnVibrate.setOnClickListener(this);
		btnLight.setOnClickListener(this);
		btnRingAndVibrate.setOnClickListener(this);
	}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:id="@+id/btnSmile" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="我今天非常高兴"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/smile" />
	<Button android:id="@+id/btnWhy" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="这是为什么呢?"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/why" />
	<Button android:id="@+id/btnWrath" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="今天心情不好"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/wrath" />

	<Button android:id="@+id/btnRing" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的声音" />
	<Button android:id="@+id/btnVibrate" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的震动" />
	<Button android:id="@+id/btnLight" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的Light" />
	<Button android:id="@+id/btnRingAndVibrate"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:text="所有的都使用默认值" />
	<Button android:id="@+id/btnClear" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="清除通知" />
</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MobileVLCKit是一个功能强大的媒体播放框架,可以轻松地将视频和音频播放功能集成到iOS和tvOS应用程序中。下面是MobileVLCKit的一些使用详解: 1. 导入MobileVLCKit库 首先,需要将MobileVLCKit库导入到Xcode项目中。可以通过CocoaPods或手动添加库的方式进行导入。如果使用CocoaPods,只需要在Podfile文件中添加以下代码: ``` pod 'MobileVLCKit' ``` 然后在终端中运行`pod install`命令即可。 如果手动导入库,需要从VLC官网下载MobileVLCKit库并将其添加到Xcode项目中。 2. 创建VLCMediaPlayer对象 要使用MobileVLCKit播放视频和音频,需要创建一个VLCMediaPlayer对象。可以通过以下代码创建一个VLCMediaPlayer对象: ``` VLCMediaPlayer *mediaPlayer = [[VLCMediaPlayer alloc] initWithOptions:nil]; ``` 3. 设置媒体源 在创建VLCMediaPlayer对象后,需要设置要播放的媒体源。可以通过以下代码设置媒体源: ``` NSURL *mediaURL = [NSURL URLWithString:@"http://example.com/video.mp4"]; VLCMedia *media = [VLCMedia mediaWithURL:mediaURL]; [mediaPlayer setMedia:media]; ``` 这里将一个URL作为媒体源,并将其设置为VLCMediaPlayer对象的媒体属性。 4. 播放媒体 设置完媒体源后,就可以开始播放媒体了。可以通过以下代码启动播放: ``` [mediaPlayer play]; ``` 5. 控制播放 MobileVLCKit提供了一些方法来控制媒体播放,例如暂停、恢复、停止和调整音量等。以下是一些示例代码: ``` // 暂停播放 [mediaPlayer pause]; // 恢复播放 [mediaPlayer play]; // 停止播放 [mediaPlayer stop]; // 调整音量 [mediaPlayer setVolume:0.5]; ``` 6. 监听播放状态 可以通过VLCMediaPlayer的`state`属性获取当前播放状态。还可以通过监听`VLCMediaPlayerStateChangedNotification`通知来获取播放状态的变化。 ``` [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerStateChanged:) name:VLCMediaPlayerStateChangedNotification object:nil]; - (void)mediaPlayerStateChanged:(NSNotification *)notification { VLCMediaPlayer *mediaPlayer = notification.object; switch (mediaPlayer.state) { case VLCMediaPlayerStateStopped: // 媒体已停止 break; case VLCMediaPlayerStatePaused: // 媒体已暂停 break; case VLCMediaPlayerStatePlaying: // 媒体正在播放 break; default: break; } } ``` 7. 自定义UI MobileVLCKit还提供了一些自定义UI的选项,例如自定义播放器控件和字幕渲染。可以通过设置VLCMediaPlayer的`drawable`属性来自定义播放器控件。可以通过设置VLCMediaPlayer的`subtitleDisplay`属性来自定义字幕渲染。 以上是MobileVLCKit的一些使用详解,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值