android 2.2 apidemos 赏析笔记 4
16lz
2021-01-23
LauncherShortcuts.java
SEE:
1.
<activity android:name=".app.LauncherShortcuts"
android:label="@string/shortcuts">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity-alias android:name=".app.CreateShortcuts"
android:targetActivity=".app.LauncherShortcuts"
android:label="@string/sample_shortcuts">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
SO:
activity-alias,重新换个名字再次使用该activity,懒,我喜欢。。没有CreateShortcuts这个文件的
SEE:
1.
final Intent intent = getIntent();
final String action = intent.getAction();
// If the intent is a request to create a shortcut, we'll do that and exit
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setupShortcut();
finish();
return;
}
SO:
创建shortcut走到这里就完了。。可以获得激活该activity的intent
SEE:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
// Then, set up the container intent (the response to the caller)
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
// Now, return the result to the launcher
setResult(RESULT_OK, intent);
SO:
设置快捷方式时 显示的快捷方式的图标和名字和intent
PS:这里的shortcut 和 widget不一样,我之前弄混了,快捷方式相关的到这里就创建结束了。。。。。 害我纠结的。。。
MenuInflateFromXml.java
SEE:
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
mSpinner.setId(R.id.spinner);
mSpinner.setAdapter(adapter);
SO:
创建spinner的步骤 复习下
SEE:
// Create help text
mInstructionsText = new TextView(this);
mInstructionsText.setText(getResources().getString(
R.string.menu_from_xml_instructions_press_menu));
// Add the help, make it look decent
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
layout.addView(mInstructionsText, lp);
// Set the layout as our content view
setContentView(layout);
SO:
动态添加view
SEE:
public boolean onCreateOptionsMenu(Menu menu) {
// Hold on to this
mMenu = menu;
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
SO:
动态生成一个布局.<menu><item><group> 多种神奇的写法 见R.main.xxxxxx.xml 懒的要死。。 知道都有什么功能就好了。。 为什么要返回重新设置菜单样式,估计onCreateOptionsMenu只调用一次 一次 次。
IncomingMessage
SEE:
1.
private View inflateView(int resource) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return vi.inflate(resource, null);
}
SO:
不能getMenuInflater,就取个SystemService的。。。
SEE:
1.
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IncomingMessageView.class), 0);
3.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis());
4:
notif.setLatestEventInfo(this, from, message, contentIntent);
5:
nm.notify(R.string.imcoming_message_ticker_text, notif);
6:IncomingMessageView.JAVA
nm.cancel(R.string.imcoming_message_ticker_text);
SO:
1、获得notificationmananger
2、等待中的意图设置,点击通知的时候跳转到的ACTIVITY
3、通知栏上显示的图片,文字和时间。如果用2.3的黑色通知栏是看不见的,看不见的,。。。请用2.2模拟器
4、显示在下拉的通知栏里的内容,指定标题,内容,意图。
5、开始通知,指定 ID,Notification。
6、取消通知,指定 ID某字符串的值用来当ID,懒得很,但是这个确实是唯一的。好方法。
NotifyingController.java
SEE:
1.
startService(new Intent(NotifyingController.this,
NotifyingService.class));
2.
stopService(new Intent(NotifyingController.this,
NotifyingService.class));
SO:
开始服务,关闭服务
SEE:
1.
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
2.
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
SO:
service要覆盖的方法。
SEE:
1.
mCondition = new ConditionVariable(false);
2.
showNotification(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
if (mCondition.block(5 * 1000))
break;
3.
mCondition.open();
SO:
1.创建ConditionVariable并设置为false不阻塞,2.mCondition.block(5 * 1000)阻塞5秒,如果阻塞不成功跳出循环。。。3.打开所阻塞的进程。。
void block()
阻塞当前线程,直到条件为open
void block(long timeout)
阻塞当前线程,直到条件为open或超时
void open()
释放所有阻塞的线程
void close()
将条件重置为close
NotifyWithText.java
SEE:
1.
Toast.makeText(NotifyWithText.this, R.string.short_notification_text,
Toast.LENGTH_SHORT).show();
2.
Toast.makeText(NotifyWithText.this, R.string.long_notification_text,
Toast.LENGTH_LONG).show();
SO:
显示时间的长短。。LENGTH_SHORT =0, Toast.LENGTH_LONG=1,感觉上显示的时间short为一秒,LONG为两秒
StatusBarNotifications
SEE:
1.makeMoodIntent()
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotificationDisplay.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("moodimg", moodId),
PendingIntent.FLAG_UPDATE_CURRENT);
SO:
比较简写的创建方法。。。
1.
Intent.FLAG_ACTIVITY_NEW_TASK If set, this activity will become the start of a new task on this history stack.
开启的ACTIVITY将在一个新的TASK中运行。
2.
PendingIntent.FLAG_CANCEL_CURRENT == if the described PendingIntent already exists, the current one is canceled before generating a new one.
如果描述的 等待意图 已经存在,取消现在这个,创建一个新的
FLAG_NO_CREATE == if the described PendingIntent does not already exist, then simply return null instead of creating it.
如果描述的意图不存在,返回null
FLAG_ONE_SHOT == this PendingIntent can only be used once.
这个 等待意图 只能使用一次
FLAG_UPDATE_CURRENT == if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.
这个描述的 等待意图已经存在了,就替换其额外的数据在这个新的意图中
SEE:
1.
Notification notif = new Notification();
notif.contentIntent = makeMoodIntent(moodId);
CharSequence text = getText(textId);
notif.tickerText = text;
notif.icon = moodId;
SO:
新的设置方法。都是PUBLIC的。。。这。。。
SEE:
1.
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);
contentView.setTextViewText(R.id.text, text);
contentView.setImageViewResource(R.id.icon, moodId);
notif.contentView = contentView;
SO:
Remote 远程遥控, 设计模式应该是代理者模式 当没有contentView的时候使用默认的view,具体的RemoteViews的设置方法,设置名字,布局。设置具体的view。
SEE:
1.
<activity android:name=".app.LauncherShortcuts"
android:label="@string/shortcuts">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity-alias android:name=".app.CreateShortcuts"
android:targetActivity=".app.LauncherShortcuts"
android:label="@string/sample_shortcuts">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
SO:
activity-alias,重新换个名字再次使用该activity,懒,我喜欢。。没有CreateShortcuts这个文件的
SEE:
1.
final Intent intent = getIntent();
final String action = intent.getAction();
// If the intent is a request to create a shortcut, we'll do that and exit
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setupShortcut();
finish();
return;
}
SO:
创建shortcut走到这里就完了。。可以获得激活该activity的intent
SEE:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
// Then, set up the container intent (the response to the caller)
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
// Now, return the result to the launcher
setResult(RESULT_OK, intent);
SO:
设置快捷方式时 显示的快捷方式的图标和名字和intent
PS:这里的shortcut 和 widget不一样,我之前弄混了,快捷方式相关的到这里就创建结束了。。。。。 害我纠结的。。。
MenuInflateFromXml.java
SEE:
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
mSpinner.setId(R.id.spinner);
mSpinner.setAdapter(adapter);
SO:
创建spinner的步骤 复习下
SEE:
// Create help text
mInstructionsText = new TextView(this);
mInstructionsText.setText(getResources().getString(
R.string.menu_from_xml_instructions_press_menu));
// Add the help, make it look decent
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
layout.addView(mInstructionsText, lp);
// Set the layout as our content view
setContentView(layout);
SO:
动态添加view
SEE:
public boolean onCreateOptionsMenu(Menu menu) {
// Hold on to this
mMenu = menu;
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
SO:
动态生成一个布局.<menu><item><group> 多种神奇的写法 见R.main.xxxxxx.xml 懒的要死。。 知道都有什么功能就好了。。 为什么要返回重新设置菜单样式,估计onCreateOptionsMenu只调用一次 一次 次。
IncomingMessage
SEE:
1.
private View inflateView(int resource) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return vi.inflate(resource, null);
}
SO:
不能getMenuInflater,就取个SystemService的。。。
SEE:
1.
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IncomingMessageView.class), 0);
3.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis());
4:
notif.setLatestEventInfo(this, from, message, contentIntent);
5:
nm.notify(R.string.imcoming_message_ticker_text, notif);
6:IncomingMessageView.JAVA
nm.cancel(R.string.imcoming_message_ticker_text);
SO:
1、获得notificationmananger
2、等待中的意图设置,点击通知的时候跳转到的ACTIVITY
3、通知栏上显示的图片,文字和时间。如果用2.3的黑色通知栏是看不见的,看不见的,。。。请用2.2模拟器
4、显示在下拉的通知栏里的内容,指定标题,内容,意图。
5、开始通知,指定 ID,Notification。
6、取消通知,指定 ID某字符串的值用来当ID,懒得很,但是这个确实是唯一的。好方法。
NotifyingController.java
SEE:
1.
startService(new Intent(NotifyingController.this,
NotifyingService.class));
2.
stopService(new Intent(NotifyingController.this,
NotifyingService.class));
SO:
开始服务,关闭服务
SEE:
1.
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
2.
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
SO:
service要覆盖的方法。
SEE:
1.
mCondition = new ConditionVariable(false);
2.
showNotification(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
if (mCondition.block(5 * 1000))
break;
3.
mCondition.open();
SO:
1.创建ConditionVariable并设置为false不阻塞,2.mCondition.block(5 * 1000)阻塞5秒,如果阻塞不成功跳出循环。。。3.打开所阻塞的进程。。
void block()
阻塞当前线程,直到条件为open
void block(long timeout)
阻塞当前线程,直到条件为open或超时
void open()
释放所有阻塞的线程
void close()
将条件重置为close
NotifyWithText.java
SEE:
1.
Toast.makeText(NotifyWithText.this, R.string.short_notification_text,
Toast.LENGTH_SHORT).show();
2.
Toast.makeText(NotifyWithText.this, R.string.long_notification_text,
Toast.LENGTH_LONG).show();
SO:
显示时间的长短。。LENGTH_SHORT =0, Toast.LENGTH_LONG=1,感觉上显示的时间short为一秒,LONG为两秒
StatusBarNotifications
SEE:
1.makeMoodIntent()
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotificationDisplay.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("moodimg", moodId),
PendingIntent.FLAG_UPDATE_CURRENT);
SO:
比较简写的创建方法。。。
1.
Intent.FLAG_ACTIVITY_NEW_TASK If set, this activity will become the start of a new task on this history stack.
开启的ACTIVITY将在一个新的TASK中运行。
2.
PendingIntent.FLAG_CANCEL_CURRENT == if the described PendingIntent already exists, the current one is canceled before generating a new one.
如果描述的 等待意图 已经存在,取消现在这个,创建一个新的
FLAG_NO_CREATE == if the described PendingIntent does not already exist, then simply return null instead of creating it.
如果描述的意图不存在,返回null
FLAG_ONE_SHOT == this PendingIntent can only be used once.
这个 等待意图 只能使用一次
FLAG_UPDATE_CURRENT == if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.
这个描述的 等待意图已经存在了,就替换其额外的数据在这个新的意图中
SEE:
1.
Notification notif = new Notification();
notif.contentIntent = makeMoodIntent(moodId);
CharSequence text = getText(textId);
notif.tickerText = text;
notif.icon = moodId;
SO:
新的设置方法。都是PUBLIC的。。。这。。。
SEE:
1.
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);
contentView.setTextViewText(R.id.text, text);
contentView.setImageViewResource(R.id.icon, moodId);
notif.contentView = contentView;
SO:
Remote 远程遥控, 设计模式应该是代理者模式 当没有contentView的时候使用默认的view,具体的RemoteViews的设置方法,设置名字,布局。设置具体的view。
更多相关文章
- Android 比Timer更好方法
- Android中AppWidget使用方法
- Android 官方 Lambda支持方法
- android BroadcastReceiver遇到 java.lang.IllegalAccessExcepti
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android软键盘弹出时不把布局顶上去的解决方法
- Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android 4.
- Android两种播放视频的方法(SurfaceView、MediaPlayer、SeekBar)
- Android 4.0 HttpUrlConnection的getInputStream()方法总是返回