5

私はコードを持っていますが、それはうまくいかないので、誰かに助けを求めます。この特定の問題に関する情報はほとんどありません。

主な活動


public class MainActivity extends Activity {
public final int PENDING_INTENT_ID = 8;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button clickity = (Button)findViewById(R.id.alarm_button);
    clickity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.SECOND, 20);

            //Create a new PendingIntent used by the Alarm when it activates
            Intent intent = new Intent(v.getContext(), AlarmReciever.class);
            intent.setAction("SOME_AWESOME_TRIGGER_WORD");
            intent.putExtra("info", "This String shows that the info is actually sent correctly");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(v.getContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT|Intent.FILL_IN_DATA);

            //Then Create the alarm manager to send this pending intent and set the date to go off
            AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);

        }
    });

}

AlarmReciever(スペルが間違っていることに気づいていますが、それがその通りなので、私はそれを使います)


public class AlarmReciever extends BroadcastReceiver{

@Override
public void onReceive(Context c, Intent arg1) {

    //get a reference to NotificationManager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);

    //Instantiate the notification

    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification.Builder builder = new Notification.Builder(c)
                                .setTicker(tickerText)
                                .setWhen(when)
                                .setContentTitle(arg1.getStringExtra("info"))
                                .setContentText("Success!!")
                                .setAutoCancel(true);
    Notification notification = builder.getNotification();
    mNotificationManager.notify(0, notification);//note the first argument, the ID should be unique



}
}

マニフェスト


<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver 
        android:name="com.testproject.AlarmReciever"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
    </receiver>

</application>


それがすべてでなければなりません。そのまま実行しようとしていますが、何も表示されません。私はエミュレーターでそれを実行していますが、それは実際に重要です。

編集:私がそれが機能しないと言うとき、私は何もポップアップしないことを意味します。正常に動作しますが、通知がポップアップすることはありません。

問題: ここに画像の説明を入力してください

そのため、問題はAndroidに絞り込まれ、通知を無視します。問題は、理由がわからないことです-_-なので、修正できません。この問題に関する専門家は、通知を呼び出すための私のコードに何か問題があると考えていますか?それがエミュレーター上にあることは重要ですか?

4

5 に答える 5

22

私は同じ問題に遭遇しました。「newNotification()」の作成時にアイコンを指定しない場合、通知は表示されません。

于 2012-11-03T17:28:17.720 に答える
8

さて、通知について学んだ教訓。エラーが発生した理由は、imgを追加する必要があるためです。追加しないと、表示されません。ウラジミールが丁寧に指摘できたことを除いて、私が持っていた他のすべては基本的に正しかった。他の人が通知をテストするだけで同様の問題が発生する場合に備えて、これをここに投稿してください。

于 2012-07-07T03:31:24.520 に答える
2
Intent intent = new Intent(v.getContext(), AlarmReciever.class);
// intent.setAction("SOME_AWESOME_TRIGGER_WORD"); replace to:
intent.setAction("com.testproject.SOME_AWESOME_TRIGGER_WORD");

少なくとも一見のためです

UPD:

long firstTime = SystemClock.elapsedRealtime();

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, /* INTERVAL IN MS */ 20 * 1000, /* PendingIntent */ intent);
于 2012-07-07T02:01:55.260 に答える
0

これは、通知アイコンを設定していないためです。通知アイコンを設定すると、機能するはずです。ログ自体は、画像なしでは通知を送信できないと言っています。

于 2013-10-17T06:24:16.567 に答える
0

アイコンの設定を忘れたようです。少なくともデフォルトのアイコンを設定する必要があります。

これを試して..

Notification.Builder builder = new Notification.Builder(c)
                         .setTicker(tickerText)
                         .setWhen(when)
                         .setContentTitle(arg1.getStringExtra("info"))
                         .setContentText("Success!!")
                         .setAutoCancel(true)
                         .setSmallIcon(R.drawable.ic_launcher); //<--- this one
于 2014-05-08T14:05:03.637 に答える