2016-11-06 5 views
1

создать 2 приложения (APPA, AppB) от Аппа я отправить сообщение AppB, в AppB улов этого сообщение и создать уведомление, но я хочу на я щелкаю уведомление открыть мою Аппу и посмотреть мое сообщение.BroadcastReceiver намерения запуска деятельности уведомление другого приложения

APPA

public class MainActivity extends Activity { 
TextView text; 
Button send; 
Intent intent; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    text = (TextView) findViewById(R.id.editText); 
    send = (Button) findViewById(R.id.button); 


    View.OnClickListener Onlistbtn = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      intent = new Intent(view.getContext(),Main2Activity.class); 
      intent.putExtra("com.example.abc.send_messege.broadcast.Message",text.getText().toString()); 
      intent.setAction("com.example.abc.send_messege.custom_action"); 
      sendBroadcast(intent); 
     } 
    }; 

    send.setOnClickListener(Onlistbtn); 
}} 

AppB

public class MyReceiver extends BroadcastReceiver { 
private final static AtomicInteger c = new AtomicInteger(3); 
public MyReceiver() { 
} 


@Override 
public void onReceive(Context context, Intent intent) { 
    String text = intent.getStringExtra("com.example.abc.send_messege.broadcast.Message"); 
    //Intent intent1 = new Intent(context, Main2Activity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0); 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(android.R.drawable.alert_dark_frame) 
        .setContentTitle("My notification") 
        .setContentText(text) 
        .setContentIntent(pIntent) 
        .setAutoCancel(true); 

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    mNotificationManager.notify(getID(), mBuilder.build()); 


} 
public static int getID() { 
    return c.incrementAndGet(); 
}} 

AndroidManifest AppB

<receiver 
     android:name=".MyReceiver" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.example.abc.send_messege.custom_action" /> 
     </intent-filter> 
    </receiver> 

ответ

1

Ok вы можете попробовать это

Мы вы создаете PendingIntent передать новое намерение launch activity from another application (APPA)

Intent intent = new Intent(); 

//com.colisa.broadcast is the package of another app 
//com.colisa.broadcast.MainActivity is the actity to be launched 
intent.setComponent(
    new ComponentName("com.colisa.broadcast","com.colisa.broadcast.MainActivity")); 

Тогда вы передаете его PengingIntent.setContentIntent(intent)

Я не triend это, но я думаю, что вы можете intent.putExtra(key, message) и получать активность получить, что messsage через (this post)

if (null != getIntent().getExtras().getString(key)){ 
    // whatever 
} 
+0

спасибо, но почему я могу использовать старое намерение appA в appB и нужно создать новое намерение? Я пытаюсь добавить ComponentName в appA и отправить appB, но он не работает ... – izac

+0

вы получаете какую-либо ошибку? Вы можете использовать 'Intent.setFlag (FLAG_DEBUG_LOG_RESOLUTION)' для дополнительного журнала информации о намерениях разрешения –

+0

нет, но если я пытаюсь использовать старые намерения (APPA) BroadcastReceiver не может получить мой messege в AppB – izac

Смежные вопросы