2016-06-28 7 views
0

私のアプリケーションが実際にすべきことは、ダイアログボックスを開く必要があります。 他のアプリケーションは、Whatsappのテキスト。ここに私のコードです。実行中のアプリケーションを閉じてダイアログボックスを開くので、実際に必要なのは実行中の他のアプリケーションです。代わりにダイアログを閉じる必要はありません。私のアプリケーションは、実際に他のアクティビティを停止する代わりに、他のアプリケーションを介してダイアログを表示する方法

  • MainActivityが起動し動作し、通知をクリックしMainActivityが閉じ
  • スタートボタンを押すと、通知が
  • 登場しているどのように

    、ダイアログボックスは、プレスがノーカウントに追加表示されます。( FB appを使用している場合は実行中のアプリケーションを終了します)

  • このプロセスを終了するには、通知を終了するには
01私は仕事にそれを必要とするどのよう

  • 私は通知をクリックすると、ダイアログボックスは、他のアプリケーションの上に開く必要があり、実行中のアプリケーションが、それはまだ

ままみんな私を助けなければならない閉鎖されていませすべきです、 前もって感謝します。 これを実装する他の方法があれば、私にお勧めします。

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> 

答えて

0

機能あなたが探しているalityはプッシュ通知と呼ばれます。一般的にこれを行うには2つの方法があります。 1.プッシュを含む多くのことを行うGoogle Cloud Messaging(GCM)は、この古い投稿:How to add a push notification in my own android app、または2.Palse(プッシュ通知用のGCMのように機能するライブラリですが、少しセットアップが簡単:https://parse.com/tutorials/android-push-notifications

+0

私のポストをもう一度読んでください。あなたの解は私の質問とは無関係です – Sathish

+0

あなたは分かりません。別のアプリケーション上にダイアログを表示することはできません。WhatsAppやFacebookやw/eのような別のアプリケーション上のダイアログボックスと思われるものは、実際にはプッシュ通知です。これらは完全に異なる構造であり、プッシュ通知を達成するには、上で概説したように、2つの方法の1つで実装する必要があります。 – gabe3vino

+0

あなたが探していると思うのは、NotificationBuilderのaddAction機能です。まだ100%ではないGCMを統合する必要があると思いますが、最終的には検索している機能を提供するアクションを追加してBuilderを変更できます。 詳細については、https://developer.android.com/reference/android/app/Notification.Builder.html#addAction%28android.app.Notification.Action%29をご覧ください。 – gabe3vino

関連する問題