android中notification的使用 (包括线程、progressbar的使用)

在android运用中,经常可以看到在下载东西的时候可以有个进度条的玩意儿显示在状态栏中。

参考网上的资料后,自己动手做了一个,结构不一定科学,但是功能确实是实现了。

首先是两个xml文件:

第一个是主页面,上面只有一个按钮而已。

<?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" android:weightSum="1">
	<Button android:text="Notification" android:id="@+id/button1"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>

</LinearLayout>

第二个xml页面,包括一个imageview、progressbar、textview。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:weightSum="1">
	<ImageView android:id="@+id/imageView1"
		android:layout_height="wrap_content" android:layout_width="wrap_content"
		android:src="@drawable/icon"></ImageView>
	<ProgressBar style="?android:attr/progressBarStyleHorizontal"
		android:layout_height="wrap_content" android:layout_width="180dip"
		android:id="@+id/progressBar1" android:layout_marginTop="13dip" ></ProgressBar>
	<TextView android:text="TextView" android:layout_height="wrap_content"
		android:id="@+id/textView1" android:layout_width="wrap_content"
		android:textSize="12sp" android:layout_marginTop="13dip"></TextView>

</LinearLayout>
其次是两个java文件

ForAndroidActivity.java

package fover.dialog;

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.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class ForAndroidActivity extends Activity implements OnClickListener {

	private NotificationManager manager;
	private RemoteViews view;
	private Notification notification;
	private PendingIntent pIntent;
	private final static int RUN = 1;
	private final static int STOP = 0;
	private int progressNumber = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notify);
		// 取得notification的服务,这样才能控制它撒
		manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// new一个notification来设置它的发生时间,一般用当前时间。
		notification = new Notification(R.drawable.icon, "这里显示您第一眼看到这个消息的信息",
				System.currentTimeMillis());
		// 将另一个用来显示在通知栏上的layout加载进来
		view = new RemoteViews(getPackageName(), R.layout.main);
		// 设置点击通知栏后需要启动的activity
		Intent intent = new Intent(ForAndroidActivity.this, Text.class);
		// 设置PendingIntent,需要注意的是这里使用的是getActivity这个方法
		pIntent = PendingIntent.getActivity(ForAndroidActivity.this, 0, intent,
				0);
		// 发现按钮并且设置监听事件
		Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			// new一个线程用于发送消息
			new Thread(new Runnable() {

				@Override
				public void run() {
					for (int i = 0; i < 20; i++) {
						progressNumber = (i + 1) * 5;
						try {
							if (i < 19) {
								Message msg = new Message();
								msg.what = RUN;
								myHandler.sendMessage(msg);
								Thread.sleep(1000);

							} else {
								Message msg = new Message();
								msg.what = STOP;
								myHandler.sendMessage(msg);
								Thread.currentThread().interrupt();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}).start();
			break;

		default:
			break;
		}

	}

	// new一个handler来接收处理消息,并且更新界面。
	private Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case RUN:
				// 注意此法,可以用来设置加载进来的那个view的页面元素
				view.setProgressBar(R.id.progressBar1, 100, progressNumber,
						false);
				view.setTextViewText(R.id.textView1, "下载完成" + progressNumber
						+ "%");
				notification.contentView = view;
				notification.contentIntent = pIntent;
				manager.notify(0, notification);

				break;
			case STOP:
				notification.icon = R.drawable.icon;
				notification.setLatestEventInfo(ForAndroidActivity.this,
						"下载完成", "您下载的ABS130已经下载完了", pIntent);
//				进度条完成之后发出提示声音
				notification.defaults=Notification.DEFAULT_SOUND;
				manager.notify(0, notification);
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

}

另一个文件text.java

package fover.dialog;

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

public class Text extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//国际惯例,加载layout文件
		setContentView(R.layout.main);
	}

}


运行效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android,可以使用TextToSpeech(TTS)引擎将文字转换为语音。要在Notification发送通知时使用TTS读出通知内容,可以使用以下步骤: 1. 在AndroidManifest.xml文件添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> ``` 2. 在build.gradle文件添加以下依赖项: ```groovy dependencies { implementation 'com.android.support:support-v4:28.0.0' implementation 'com.google.android.gms:play-services:12.0.1' } ``` 3. 在Notification发送通知时,调用TextToSpeech引擎将通知内容转换为语音,例如: ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); TextToSpeech tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { tts.setLanguage(Locale.getDefault()); tts.speak("Hello World!", TextToSpeech.QUEUE_ADD, null); } } }); ``` 这将发送一个通知并将“Hello World!”转换为语音。请注意,上述代码只是示例,您需要根据您的需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值