【问题标题】:how to display dialog over other application instead of stopping other activity如何在其他应用程序上显示对话框而不是停止其他活动
【发布时间】:2016-10-31 06:11:37
【问题描述】:

我的应用程序实际上应该做的是,它应该打开一个对话框 其他应用程序说,就像 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>

【问题讨论】:

    标签: android android-layout android-activity dialog


    【解决方案1】:

    您正在寻找的功能称为推送通知。通常有两种方法可以做到这一点; 1. Google Cloud Messaging (GCM),它做了很多事情,包括推送,这样做的方法可以在这篇较早的帖子中找到:How to add a push notification in my own android app,或者 2. Parse,这是一个类似于 GCM 的库,用于推送通知但设置起来更容易:https://parse.com/tutorials/android-push-notifications

    【讨论】:

    • 能否请您再看一遍我的帖子。您的解决方案与我的问题无关
    • 不,你不明白;您不能在另一个应用程序上显示对话框。您认为是另一个应用程序(例如 WhatsApp 或 Facebook 或 w/e)上的对话框,实际上是推送通知。它们是完全不同的结构,要实现推送通知,您需要按照我上面概述的 2 种方式之一来实现它。
    • 我认为您正在寻找的是 NotificationBuilder 中的 addAction 功能。我认为您仍然需要集成 GCM,不是 100% 肯定,但是您最终可以使用那些应该提供您正在搜索的功能的添加操作来修改该 Builder。详情见developer.android.com/reference/android/app/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多