2

Onreceive 方法会在 context.startActivity(Intent.createChooser(sendIntent, "Send")); 它在 oncreate 方法中工作正常,但在广播接收器类中不起作用。有谁能知道原因。我是新的安卓程序员。提前致谢。

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> 
4

1 回答 1

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 相关的上下文(如服务或广播接收器)之外启动 Activity 时,您需要将 Activity 附加FLAG_ACTIVITY_NEW_TASK到意图。

于 2013-03-05T13:08:08.483 回答