天气预报--插件功能完善

看到好多插件上都有些按下监听,我也想给自己的插件加一些功能呢,最近项目写完了真是各种闲啊哈哈哈~

点击天气图标打开自己的app和点击时间图片打开系统闹钟。自从写了插件我才明白AppWidgetProvider的onUpdate方法是最重要的呢,几乎所有重要的事务代码都写在这个方法里面,同理,如果我想实现点击图片打开app怎么做,因为插件和应用是不在同一个进程中的,所以平常在Activity中的监听方法不管用了,但是intent还是有用的,于是在onUpdate方法中可以这样写

for(int i=0;i<appWidgetIds.length;i++){
			Intent intent=new Intent(context,MainInterface.class);
			//创建一个PendingIntent
			PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
			RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
			remoteViews.setOnClickPendingIntent(R.id.widget_image,pendingIntent);
			appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
					
		}

终于知道了上次的RemoteViews到底有什么用了,它是为了使与主程序不在同一个进程中的widget能使用操作控件

然后PendingIntent的话也是应用在不同进程中的,资料来自:http://dong2008hong.blog.163.com/blog/static/469688272013112734658641/

这样就实现了点击widget_image开启我们的主Activity


然后知道了1就知道2了嘛,启动闹钟反正也逃不出这个方法,各种找也是知道了,差不多方法,只不过要在manifest中加一条权限:

 
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <!-- 添加系统闹钟权限   -->

然后再import android.provider.AlarmClock;这个包

最后就和上面差不多啦~


for(int i=0;i<appWidgetIds.length;i++){
		//这里实现了如何点击插件上的时间打开闹钟
		Intent intentAlarm=new Intent(AlarmClock.ACTION_SET_ALARM);
		PendingIntent pendingIntentAlarm = PendingIntent.getActivity(context, 0, intentAlarm, 0);
		RemoteViews remoteViewsAlarm = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
		remoteViewsAlarm.setOnClickPendingIntent(R.id.time,pendingIntentAlarm);
		appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViewsAlarm);
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值