在下面启动一个没有主应用程序的对话框

时间:2012-08-16 02:50:35

标签: android

我构建了一个具有活动的应用程序,我希望从点击状态通知图标开始。我想要发生的是我的活动,我将其设置为一个对话框,显示在设备上已打开的任何应用程序之上。

但现在发生的事情是,当点击通知图标时,对话框活动将与其下方的应用程序主要活动一起启动,如下图所示。

http://postimage.org/image/s40v1588b/

如何启动对话框以使其显示在屏幕上的当前应用程序中并且我的主要活动未启动?

主要活动:

public class mainActivity extends Activity {
    public  NotificationManager mNotificationManager;
    private Button start;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        start = (Button) findViewById(R.id.start);
        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getApplicationContext(), servicetest.class);
                startService(myIntent);
                Log.d("start","sms");
                Toast.makeText(SmseverywhereActivity.this, "Service Started",
                        Toast.LENGTH_LONG).show();
            }

        });

    }

}

侦听通知按下并启动对话活动的服务:

    public class servicetest extends Service{
        public  NotificationManager mNotificationManager;
        public Intent dialogIntent;

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("servicetest","onCreate");
            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            showNotification();


            }


         private void showNotification(){
                Notification not = new Notification(R.drawable.ic_launcher, "Application started", System.currentTimeMillis());
                Intent myIntent = new Intent(this, dialogactivity.class);
                myIntent.putExtra("isFullScreen", true);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myIntent, Notification.FLAG_ONGOING_EVENT);        
                not.flags = Notification.FLAG_ONGOING_EVENT;            
                not.setLatestEventInfo(this, "Application Name", "Application Description", contentIntent);
                mNotificationManager.notify(1, not);
            }



    }

Dialog activity:
public class dialogactivity extends Activity{
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.dialogtest);

     }
}

清单:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SmseverywhereActivity"
            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:label="@string/app_name"
            android:name="NewText"
            android:theme="@android:style/Theme.Dialog"
            android:launchMode="singleTask"
            android:screenOrientation="user">
        </activity>

        <service android:enabled="true" android:name=".servicetest" />

1 个答案:

答案 0 :(得分:0)

我发现了我的问题,我需要从我的主要活动中拨打finish();,以便每次启动其他活动时都不会重新启动。