来自broadcastreceiver的StartActivity

时间:2013-03-05 13:05:40

标签: android

Onreceive方法会在context.startActivity(Intent.createChooser(sendIntent,“Send”))中崩溃应用程序; 它在oncreate方法中工作正常,但在broadcastreceiver类中不起作用。  任何人都可以知道原因。我是新的android程序员。 提前谢谢。

public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {

    takePictureNoPreview(context);    
                String imagePath="test.jpeg";
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
            //    sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
                sendIntent.putExtra("sms_body", "I Send you pic.");

                sendIntent.putExtra("address", phoneNumber);
                sendIntent.setType("image/png");
                sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
       //         context.startActivityForResult(sendIntent, 1);
                context.startActivity(Intent.createChooser(sendIntent, "Send"));
      //          context.startActivity(sendIntent);


            }       
        }
}        

        public void takePictureNoPreview(Context context){
            // open back facing camera by default
            Camera myCamera= Camera.open();//openFrontFacingCameraGingerbread();

            if(myCamera!=null){
              try{
                //set camera parameters if you want to
                //...

                // here, the unused surface view and holder
                SurfaceView dummy=new SurfaceView(context);
                myCamera.setPreviewDisplay(dummy.getHolder());    
                myCamera.startPreview(); 

                myCamera.takePicture(null, null, photoCallback);


              } catch (IOException e) {

                e.printStackTrace();
            }finally{
       //         myCamera.close();
              }      

            }else{
                 Toast toast = Toast.makeText(context, "No front Facing camera", Toast.LENGTH_LONG);
                 toast.show();
            }
        }

        Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                // Save the image JPEG data to the SD card
                FileOutputStream fos = null;        
            //    camera.startPreview();

                bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
                StudentDemo.iView.setImageBitmap(bmp);
                 try {
                     fos = new FileOutputStream(dataFile,true);              
                     fos.write(data);
                     fos.close();

                } catch (FileNotFoundException e) {             
                    e.printStackTrace();
                } catch (IOException e) {           
                    e.printStackTrace();
                }

                /*new SavePhotoTask().execute(data);
                camera.startPreview();*/
            }
        };

}

Android清单文件是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gen.stud"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-sdk android:minSdkVersion="9"
        android:targetSdkVersion="9" 
        />

    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera.autofocus" />
   <uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Demo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".StudentDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.StudentDemo" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>


</manifest> 

1 个答案:

答案 0 :(得分:3)

尝试:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body", "I Send you pic.");
sendIntent.putExtra("address", phoneNumber);
sendIntent.setType("image/png");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sendIntent, "Send"));

每当从UI相关上下文(如Service或BroadcastReceiver)外部启动Activity时,您需要将FLAG_ACTIVITY_NEW_TASK附加到intent。