如何在其他应用程序上显示对话框而不是停止其他活动

时间:2016-06-28 19:03:20

标签: android android-layout android-activity dialog

我的应用程序实际应该做的是,它应该打开一个对话框 其他应用程序说像Whatsapp的通知功能文本,就像它打开任何类似应用程序的窗口。这是我的代码。它会关闭正在运行的应用程序并打开对话框,因此我实际需要的是正在使用的其他正在运行的应用程序不应该关闭,而是应该打开对话框。

我的申请实际如何运作

  • MainActivity启动并按下开始按钮
  • MainActivity关闭并显示通知
  • 点击通知,出现对话框按添加计算编号(这里关闭正在运行的应用程序,如果我正在使用FB应用程序)
  • 要完成此过程,请按通知中的退出

我需要它如何工作

  • 当我点击通知时,对话框应该在另一个应用程序上打开,正在运行的应用程序不应该关闭它应该保持不变

帮帮我们,谢谢你们。 建议我是否有其他方法来实现这一点。

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>

1 个答案:

答案 0 :(得分:0)

您正在寻找的功能称为推送通知。通常有两种方法可以做到这一点; 1.谷歌云消息传递(GCM),它执行许多操作,包括推送,这样做的方法可以在这篇较老的帖子中找到:How to add a push notification in my own android app,或2.解析,这是一个像GCM一样运行的库推送通知,但设置起来更容易:https://parse.com/tutorials/android-push-notifications