Thursday, March 27, 2014

Again ask people.


Android recently looking at the books, there are two ways pop-up page (currently only seen two kinds).

One is a direct call class by class to display the page.
startActivityForResult (new Intent (this, TY.class), requestCode);

The other is the broadcast? Specific code as follows, requires three classes.
MainActivity categories:
public class MainActivity extends Activity {
private Button btn;
private static final String MY_ACTION = "com.example.notification2.MY_ACTION";
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
btn = (Button) findViewById (R.id.button1);
btn.setOnClickListener (listener);
}

private OnClickListener listener = new OnClickListener () {
@ Override
public void onClick (View v) {
Intent intent = new Intent ();
intent.setAction (MY_ACTION);
sendBroadcast (intent);
}
};
}

MyReceiver categories:
public class MyReceiver extends BroadcastReceiver {
@ Override
public void onReceive (Context context, Intent intent) {
Intent i = new Intent ();
i.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass (context, DisplayActivity.class);
context.startActivity (i);
}
}
DisplayActivity categories:
public class DisplayActivity extends Activity {
private Button cancelBtn;
private Notification n;
private NotificationManager nm;
private static final int ID = 1;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main2);
cancelBtn = (Button) findViewById (R.id.button2);
String service = NOTIFICATION_SERVICE;
nm = (NotificationManager) getSystemService (service);

n = new Notification ();
int icon = n.icon = android.R.drawable.ic_dialog_alert;
String tickerText = "notice Test Notification";
long when = System.currentTimeMillis ();
n.icon = icon;
n.tickerText = tickerText;
n.when = when;

Intent intent = new Intent (this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity (this, 0, intent, 0);
n.setLatestEventInfo (this, "My Title", "My Content", pi);
nm.notify (ID, n);
cancelBtn.setOnClickListener (cancelListener);
}

private OnClickListener cancelListener = new OnClickListener () {
@ Override
public void onClick (View v) {
nm.cancel (ID);
}
};

}


I do not understand the way these two calls, what's the difference?
Or that I did not understand understand?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Another XML, is written.

package = "com.example.notification2"
android: versionCode = "1"
android: versionName = "1.0">

android: minSdkVersion = "8"
android: targetSdkVersion = "17" />

android: allowBackup = "true"
android: icon = "@ drawable / ic_launcher"
android: label = "@ string / app_name"
android: theme = "@ style / AppTheme">
android: name = "com.example.notification2.MainActivity"
android: label = "@ string / app_name">















Reply:
I actually do not know what your problem is.
Reply:
These two methods is the same, ah, are startActivity.
The difference is that the first one is to start Activity Activity initiative in the current trigger, the second is a received broadcast later.
Reply:
Asked a seemingly weak weak. Thank you upstairs friends.

No comments:

Post a Comment