I built an application that has an activity that i want to start from the click of a status notification icon. What i want to happen is for my activity which i set to be shown as a dialog box to show up on top of whatever application is already open on the device.
But what happens now is that when the notification icon is clicked the dialog activity is launched along with the applications main activity underneath it, as seem in the image bellow.
http://postimage.org/image/s40v1588b/
How can i launch the dialog box so that it shows up over the current application on screen and my main activity isn’t launched?
Main Activity:
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();
}
});
}
}
Service that listens for notification press and starts dialog activity:
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);
}
}
manifest:
<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" />
I figured out my problem, I needed to call
finish();from my main activity so that it wasn’t relaunched again every time my other activities were launched.