在android中使用ACTION_TIME_TICK [英] Use ACTION_TIME_TICK in android

查看:759
本文介绍了在android中使用ACTION_TIME_TICK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我写这段代码但是无法安装。

在行中的清单文件中< service name =.....>它显示警告导出的服务不需要许可,当我安装小部件时,它正在关闭安装

我该如何解决?

谢谢



  import  java。 util.Calendar; 

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {


@ Override
public void onUpdate(上下文上下文,AppWidgetManager appWidgetManager, int [] appWidgetIds){
super .onUpdate(context,appWidgetManager,appWidgetIds);
}

@覆盖
public void onEnabled(上下文上下文){
super .onEnabled(context);
}

@覆盖
public void onDisabled(上下文上下文){
super .onDisabled(context);
}

public final static class UpdateService extends 服务
{

final static String ACTION_UPDATE = ir.ashouraiyan.UPDATE;

final static IntentFilter intentFilter;
日历日历;
static
{
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}

@覆盖
public IBinder onBind (意图arg0){
return null;
}

@覆盖
public void onCreate(){
super .onCreate();
Init();
registerReceiver(timeChangedReceiver,intentFilter);
}

@覆盖
public void onDestroy(){
super .onDestroy();
unregisterReceiver(timeChangedReceiver);
}

@覆盖
public int onStartCommand(Intent intent, int 标志, int startId){
if (ACTION_UPDATE.equals(intent.getAction()))
Update();
return super .onStartCommand(intent,flags,startId);
}
void Init()
{
calendar = Calendar.getInstance();
}

void 更新()
{
calendar.setTimeInMillis(System.currentTimeMillis()) ;
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.widget_layout);

remoteViews.setTextViewText(R.id.textView, + calendar .getTimeInMillis());

ComponentName widget = new ComponentName( this ,WidgetProvider。);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance( this );
appWidgetManager.updateAppWidget(widget,remoteViews);
}

final BroadcastReceiver timeChangedReceiver = new BroadcastReceiver(){

@ Override
public void onReceive(上下文上下文,意图意图){
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)||
action.equals(Intent.ACTION_TIME_CHANGED))
Init() ;

Update();
}

};

}

}







< pre lang =xml> <? xml version = 1.0 encoding = utf-8 >
< manifest xmlns:android = http://schemas.android.com/apk/res/ android

package = ir.ashouraiyan.moharamdaycounter

android :versionCode = 1

android:versionName = 1.0 xmlns:tools = http://schemas.android.com/tools >

< uses-sdk

android:minSdkVersion = 7

< span class =code-attribute> android:targetSdkVersion = 18 / >

< application

android:allowBackup = true

android:icon = @ drawable / ic_launcher

android:labe l = @ string / app_name

android:主题 = @ style / AppTheme >

< receiver

android:姓名 = 。小工具提供者 >
< intent-filter >
< action android:name = android .appwidget.action.APPWIDGET_UPDATE / >
< / intent-filter >
< 元数据
< span class =code-attribute>
< span class =code-attribute> android:name = android.appwidget.action.APPWIDGET_UPDATE / >
< / receiver >

< service android:name = 。WidgetProvider $ UpdateService >
< intent -filter >
< 行动 android:name = ir.ashouraiyan.UPDATE / >
< / intent-filter >
< / service >

< / application > ;

< / manifest >

解决方案

UpdateService >
< intent-filter >
< action android:name = ir.ashouraiyan.UPDATE / >
< / intent-filter < span class =code-keyword>>
< / service >

< / application >

< / manifest >


Hi.
I write this code but it cant install.
In manifest file in line <service name="....."> it show a warning "Exported service does not require permission" and when I install the widget it is closing instalation
How can I solve it ?
thanks

import java.util.Calendar;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider{
	
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
	}
	
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
	}
	
	public final static class UpdateService extends Service
	{

		final static String ACTION_UPDATE = "ir.ashouraiyan.UPDATE";
		
		final static IntentFilter intentFilter;
		Calendar calendar;
		static 
		{
			intentFilter = new IntentFilter();
			intentFilter.addAction(Intent.ACTION_TIME_TICK);
			intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
			intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
		}
		
		@Override
		public IBinder onBind(Intent arg0) {
			return null;
		}
		
		@Override
		public void onCreate() {
			super.onCreate();
			Init();
			registerReceiver(timeChangedReceiver, intentFilter);
		}
		
		@Override
		public void onDestroy() {
			super.onDestroy();
			unregisterReceiver(timeChangedReceiver);
		}
		
		@Override
		public int onStartCommand(Intent intent, int flags, int startId) {
			if(ACTION_UPDATE.equals(intent.getAction()))
				Update();
			return super.onStartCommand(intent, flags, startId);
		}
		void Init()
		{
			calendar = Calendar.getInstance();
		}
		
		void Update()
		{
			calendar.setTimeInMillis(System.currentTimeMillis());
			RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout);
			
			remoteViews.setTextViewText(R.id.textView, ""+calendar.getTimeInMillis());
			
			ComponentName widget = new ComponentName(this, WidgetProvider.class);
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
			appWidgetManager.updateAppWidget(widget, remoteViews);
		}
		
		final BroadcastReceiver timeChangedReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				final String action = intent.getAction();
				if(action.equals(Intent.ACTION_TIMEZONE_CHANGED) ||
				   action.equals(Intent.ACTION_TIME_CHANGED))
					Init();
			
				Update();
			}
			
		};
		
	}
	
}




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="ir.ashouraiyan.moharamdaycounter"

    android:versionCode="1"

    android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools">

    <uses-sdk

        android:minSdkVersion="7"

        android:targetSdkVersion="18" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >
        
        <receiver 

            android:name=".WidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data 

                android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </receiver>
        
        <service android:name=".WidgetProvider$UpdateService">
            <intent-filter>
                <action android:name="ir.ashouraiyan.UPDATE"/>
            </intent-filter>
        </service>
        
    </application>

</manifest>

解决方案

UpdateService"> <intent-filter> <action android:name="ir.ashouraiyan.UPDATE"/> </intent-filter> </service> </application> </manifest>


这篇关于在android中使用ACTION_TIME_TICK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆