我是Android的新手,如果有人会帮我解决这个问题,我将不胜感激。我有一个应用程序,当收到短信时,它应该拍摄视频。但它不起作用。这是代码: SmsReceiver类:
public class SmsReceiver extends BroadcastReceiver{ static int ok=0;
@Override
public void onReceive(Context context, Intent intent)
{ Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String mesaj="";
if (bundle != null)
{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 += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
mesaj = msgs[i].getMessageBody().toString();
str += "\n";
}if(mesaj.equals("Video")){Toast.makeText(context, mesaj, Toast.LENGTH_LONG).show();
ok=1;} } } }
MainActivity类:
public class MainActivity extends Activity implements SurfaceHolder.Callback{Button myButton; MediaRecorder mediaRecorder; SurfaceHolder surfaceHolder; boolean recording; int dur=3000; static MainActivity video;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
mediaRecorder = new MediaRecorder();
initMediaRecorder();
video=this;
setContentView(R.layout.activity_main);
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);} public Button.OnClickListener myButtonOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
if(SmsReceiver.ok==1) if(recording){ mediaRecorder.stop(); mediaRecorder.release();}else{mediaRecorder.start();recording = true; myButton.setText("STOP");} }};@Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}@Override public void surfaceCreated(SurfaceHolder arg0) {prepareMediaRecorder();}@Override public void surfaceDestroyed(SurfaceHolder arg0) { }public void initMediaRecorder(){mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".mp4");
mediaRecorder.setMaxDuration(3000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M}public void prepareMediaRecorder(){mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());try{mediaRecorder.prepare();} catch (IllegalStateException e) {e.printStackTrace();}catch (IOException e){e.printStackTrace();}}}