1

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

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

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

1 回答 1

0

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

于 2012-08-16T19:32:27.233 回答