私はAndroidプログラミングの初心者で、以下のコードに出くわしました。
public void email(View view){
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext())
.setTicker("NEW EMAIL!!!")
.setSmallIcon(android.R.drawable.ic_dialog_email)
.setAutoCancel(true)
.setContentText("This is a new E-mail!!!")
.setContentIntent(pendingIntent);
NotificationManager manager =(NotificationManager)getSystemService(
Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
のインスタンスを作成することが次のようになっているのを見て、私は腹を立てましたPendingIntent
。
PendingIntent.getActivity(etc.)
のインスタンスの作成NotificationManager
は次のとおりです。
(NotificationManager)getSystemService(etc.)
のインスタンスを作成している間NotificationCompat.Builder
は次のとおりです。
new NotificationCompat.Builder(getApplicationContext())
「新しい」という言葉や方法getSystemService()
などをいつ使うべきかをどうやって知ることができますか。インスタンス化の普遍的なルールはありますか、それともクラスごとに1つずつ学習する必要がありますか?
ご協力いただきありがとうございます!
Androidフレームワークのクラスの多くは、次を使用してインスタンス化できますnew
。
Fragment f = new Fragment();
LinearLayout l = new LinearLayout();
Intent i = new Intent();
次に、プログラマがを使用してインスタンス化してはならない他のクラスがありますnew
。Activity
例です。Application
別です。
あなたが引用した例では、ビルダーという用語はビルダーパターンへの参照です。このPendingIntent
クラスには、プログラマーが指定したタイプの新しいメソッド、つまりforまたはをフェッチするためのメソッドがgetActivity()
あります。getService()
PendingIntent
Intent
Activity
Service
経験を積むとnew
、いつ直接使用するか、いつシングルトンを使用するかなどについて詳しく学ぶことができます。これらは、効率とコードの再利用を向上させることを目的としたJavaの規則です。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加