我提到了两个链接:
从这两个链接中,我创建了一个 AppWidgetProvider 类和一个 Service 类,如下所示:
public class HomeWidget extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName thisWidget = new ComponentName(context,
HomeWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent homeIntent = new Intent(context.getApplicationContext(), VoiceService.class);
homeIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(homeIntent);
}
}
服务:
public class VoiceService extends Service implements OnInitListener{
public static TextToSpeech mtts;
private static final String LOG = "VoiceService";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(LOG,"Service created successfully!");
mtts = new TextToSpeech(getApplicationContext(),this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mtts!=null)
{
mtts.stop();
Log.d(LOG,"The service has been destroyed!");
}
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
HomeWidget.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for(int widgetId : allWidgetIds)
{
Log.w(LOG,"inside for loop");
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.home_widget);
mtts = new TextToSpeech(getApplicationContext(),this);
mtts.setLanguage(Locale.ENGLISH);
mtts.speak("This is the text to be spoken", TextToSpeech.QUEUE_FLUSH, null);
Intent clickIntent = new Intent(this.getApplicationContext(),HomeWidget.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.home, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
我启动了小部件,当我点击小部件时,logcat 中出现了这 6 行:
08-03 02:12:55.354: D/VoiceService(3666): Service created successfully!
08-03 02:12:55.358: W/VoiceService(3666): From Intent1
08-03 02:12:55.358: W/VoiceService(3666): Direct1
08-03 02:12:55.358: W/VoiceService(3666): inside for loop
08-03 02:12:55.358: I/TTS received:(3666): This is the text to be spoken
08-03 02:12:55.370: D/VoiceService(3666): The service has been destroyed!
但我什么也没听到。我已将音量调到最大,确保它使用的是语音合成引擎(Pico TTS)并为其安装了英语(美国)和英语(英国)。我还安装了语音合成所需的语音数据。
我还有什么遗漏或做错了什么以至于没有声音被说出来?