What my application actually should do is, it should open a dialog box over other application say like Whatsapp's text from notification feature like it opens a window over any application like that. Here is my code. It closes an running application and opens the dialog box so what i actually need is the other running application am using should not get closed instead the dialog should open over them.
How my application actually works
- MainActivity starts and press the start button
- MainActivity closes and the notification is appeared
- Click the notification, dialog box appears press add to count the no.(Here it closes the running application say if I'm using FB app)
- To finish the process press quit in the notification
How I need it to work is
- When i click the notification the dialog box should open over the other application the running application should not get closed it should remain still
Help me out guys, Thanks in advance. Suggest me if any other ways to implement this.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationClass notification = new NotificationClass(MainActivity.this);
notification.showNotification("Floating app","Started");
finish();
}
});
}
NotificationClass.java
public class NotificationClass {
Context mContext;
static int NOTIFICATION_ID = 111;
NotificationClass(Context context){
mContext = context;
}
void showNotification(String title,String message)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setSmallIcon(R.drawable.ic_stat_name).setOngoing(true).setContentTitle(title).setContentText(message);
Intent activityIntent = new Intent(mContext,WorkSpace.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(activityIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(123,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//
Intent resultIntent = new Intent(mContext,ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,456,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.ic_stat_cancel,"Quit",resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
void cancelNotification(){
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
}
ResultActivity.java
public class ResultActivity extends AppCompatActivity {
TextView tvResult;
SharedPreferences sp;
String PRESENT = "isPresent";
String COUNT = "getCount";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tvResult = (TextView) findViewById(R.id.tvResult);
NotificationClass notificationClass = new NotificationClass(this);
notificationClass.cancelNotification();
sp = PreferenceManager.getDefaultSharedPreferences(this);
String msg = "Total Count:"+sp.getInt(COUNT,-3);
tvResult.setText(msg);
resetCount();
}
private void resetCount() {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(PRESENT,false);
editor.putInt(COUNT,0);
editor.apply();
editor.commit();
}
}
WorkSpace.java
public class WorkSpace extends AppCompatActivity {
String TAG = "WorkSpace";
TextView tvCount;
Button btAdd;
Integer count;
SharedPreferences sp;
String PRESENT = "isPresent";
String COUNT = "getCount";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"Created");
setContentView(R.layout.activity_work_space);
tvCount = (TextView) findViewById(R.id.tvCount);
btAdd = (Button) findViewById(R.id.btAdd);
sp = PreferenceManager.getDefaultSharedPreferences(this);
}
public void addCount(View v){
count++;
updateCount();
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"Paused");
storeCount();
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"Resumed");
if(sp.getBoolean(PRESENT,false)){
count = sp.getInt(COUNT,-1);
}else{
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(PRESENT,true);
editor.putInt(COUNT,0);
editor.apply();
editor.commit();
count = sp.getInt(COUNT,-2);
}
updateCount();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"Destroyed");
}
private void updateCount() {
String msg = "Count:"+count;
tvCount.setText(msg);
}
private void storeCount() {
SharedPreferences.Editor editor = sp.edit();
editor.putInt(COUNT,count);
editor.apply();
editor.commit();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kewldevs.sathish.floatapps.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/start"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_result.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kewldevs.sathish.floatapps.ResultActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:id="@+id/tvResult"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_work_space.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kewldevs.sathish.floatapps.WorkSpace"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/tvCount"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/btAdd"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="addCount" />
</LinearLayout>
Manifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity" />
<activity android:name=".WorkSpace"
android:theme="@style/Theme.AppCompat.Dialog"
android:excludeFromRecents="true">
</activity>
</application>