应用程序不工作

时间:2010-06-15 00:46:49

标签: android

我写了一个简单的应用程序,可以向用户说出任何传入的消息。当我把它们作为两个单独的pgms推出时,这两个程序似乎都工作得很好,但是在将它们保持在同一个项目/包中时,只能看到扬声器程序屏幕并且接收器pgm似乎不起作用。有人可以帮我解决一下吗?

发言人pgm是:

package com.example.TextSpeaker;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

// the following programme converts the msg user to speech

public class TextSpeaker extends Activity  implements OnInitListener {
/** Called when the activity is first created. */
int MY_DATA_CHECK_CODE = 0;
public TextToSpeech mtts;
public Button button;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){ 

 @Override
  public void onClick(View v) {
  Toast.makeText(getApplicationContext(), "The service has been started\n Every new   message will now be read out", Toast.LENGTH_LONG).show();

   }

    });
    Intent myintent = new Intent();
    myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(myintent, MY_DATA_CHECK_CODE);
}
    protected void onActivityResult(int requestcode,int resultcode,Intent data)
    {
     if(requestcode == MY_DATA_CHECK_CODE)
     {
      if(resultcode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
      {
       // success so create the TTS engine
       mtts = new TextToSpeech(this,this);
       mtts.setLanguage(Locale.ENGLISH);

      }
      else
      {
       //install the Engine
       Intent install = new Intent();
       install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
       startActivity(install);
      }
     }

    }
    public void onDestroy(Bundle savedInstanceStatBundle)
    { 
     mtts.shutdown();
    }

    public void onPause()
    { 
     super.onPause();
     // if our app has no focus
     if(mtts!=null)
      mtts.stop();
    }
 @Override
 public void onInit(int status) {
 if(status==TextToSpeech.SUCCESS)
  button.setEnabled(true);

}


}

和Receiver程序是:

package com.example.TextSpeaker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage; // supports both gsm and cdma
import android.widget.Toast;



public class Receiver extends BroadcastReceiver{


 @Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str="";
if(bundle!=null)
{
// retrive the sms received

Object[] pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
str+="Message From "+msgs[i].getOriginatingAddress()+".";
str+="Message "+msgs[i].getMessageBody().toString();
}
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
TextSpeaker tsp  = new TextSpeaker();
tsp.mtts.speak(str, TextToSpeech.QUEUE_ADD,null);

}

}

}

1 个答案:

答案 0 :(得分:0)

我猜tsp.mtts为空。您正在直接创建TextSpeaker的新实例,而不是操作系统正确创建的活动,因此没有理由调用其onCreate方法,因此永远不会初始化mtts