Android:从服务内部启动SMS /电子邮件应用程序

时间:2012-08-09 21:53:44

标签: android service

我想从我的服务运行中启动SMS和电子邮件应用程序。下面的代码在Activity中使用时工作,但是在Service中它给出了错误。请注意,如果我开始简单的活动,它可以在服务中正常工作。

subject="My currunt Position";
Intent intentemail = new Intent(Intent.ACTION_SEND);
intentemail.setType("message/rfc822");
intentemail.putExtra(Intent.EXTRA_SUBJECT, subject);
intentemail.putExtra(Intent.EXTRA_TEXT, returnMessageBody());
Intent mailer = Intent.createChooser(intentemail,  null);
mailer.addFlags(FLAG_ACTIVITY_NEW_TASK);
startActivity(mailer);

Logcat跟踪

08-10 02:32:21.106: E/AndroidRuntime(1202): FATAL EXCEPTION: main
08-10 02:32:21.106: E/AndroidRuntime(1202): java.lang.RuntimeException: Unable to start      service learn2develop.GoogleMaps.WidgetService@4051bf40 with Intent { act=Send_Sms_Call   flg=0x10000000 cmp=learn2develop.GoogleMaps/.WidgetService bnds=[210,372][300,522] }:  android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity   context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-10 02:32:21.106: E/AndroidRuntime(1202):     at  android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
08-10 02:32:21.106: E/AndroidRuntime(1202):     at  android.app.ActivityThread.access$2800(ActivityThread.java:117)
08-10 02:32:21.106: E/AndroidRuntime(1202):     at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
08-10 02:32:21.106: E/AndroidRuntime(1202):     at android.os.Handler.dispatchMessage(Handler.java:99)

清单文件

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

<uses-sdk android:minSdkVersion="4" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" android:name="GoogleMapApplication">

    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".GoogleMapsActivity"
        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="mainPrefferenceActivity"></activity>
    <activity android:name="ShowMapActivity" android:label="@string/app_name"></activity>
    <receiver android:name=".GoogleMapsWidgetProvider" android:label="Google Map">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="learn2develop.GoogleMaps.GoogleMapsWidgetProvider.ACTION_WIDGET_SERVICETOACTIVITY" />

         </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/app_widget_provider" />
        </receiver>
  <activity android:name=".WidgetActivity">

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
                            </intent-filter>
    </activity>
    <activity android:name=".refreshWidgetActivity">

        <intent-filter>
        </intent-filter>
    </activity>
    <service android:name=".WidgetService">

        <intent-filter>
            <action android:name="learn2develop.GoogleMaps.WidgetActivity.ACTION_WIDGET_REFRESH" />
        </intent-filter>
    </service>
    <activity android:name=".WidgetSmsActivity">


    </activity>
    <activity android:name=".WidgetEmailActivity">
        <action android:name="learn2develop.GoogleMaps.GoogleMapsWidgetProvider.ACTION_WIDGET_EMAIL" />

    </activity>

</application>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

0 个答案:

没有答案